instanceof

Why does instanceof return false for some literals?

元气小坏坏 提交于 2019-12-07 19:18:32
问题 "foo" instanceof String //=> false "foo" instanceof Object //=> false true instanceof Boolean //=> false true instanceof Object //=> false false instanceof Boolean //=> false false instanceof Object //=> false // the tests against Object really don't make sense Array literals and Object literals match... [0,1] instanceof Array //=> true {0:1} instanceof Object //=> true Why don't all of them? Or, why don't they all not ? And, what are they an instance of, then? It's the same in FF3, IE7,

Generics and instanceof - java

好久不见. 提交于 2019-12-07 10:10:56
问题 OK this is my class, it encapsulates an object, and delegates equals and to String to this object, why I can´t use instance of??? public class Leaf<L> { private L object; /** * @return the object */ public L getObject() { return object; } /** * @param object the object to set */ public void setObject(L object) { this.object = object; } public boolean equals(Object other) { if(other instanceof Leaf<L>) //--->ERROR ON THIS LINE { Leaf<L> o = (Leaf<L>) other; return this.getObject().equals(o

How to get rid of instanceof in this Builder implementation

不想你离开。 提交于 2019-12-07 05:09:52
问题 The idea I need to create Commands. Commands can be configured with parameters. Not every command can receive the same parameters. So some have to be ignored. I have an abstract class Command in which I've defined a Builder. By default every append parameter throws 'UnsupportedOperationException' public abstract class Command { public static abstract class CommandBuilder { // TODO instanceof. How to do this better? public CommandBuilder append(Parameter p) throws UnsupportedOperationException

Java: Unchecked cast from X to Y / how to implement castOrNull

跟風遠走 提交于 2019-12-07 02:48:50
问题 I have implemented this function: static <X,Y> Y castOrNull(X obj) { try { return (Y)obj; } catch(ClassCastException e) { return null; } } This gives me the compiler warning: Type safety: Unchecked cast from X to Y Which I don't exactly understand. Isn't the try/catch which I am doing here a check for it? Can I ignore the warning? Will my function work as expected or not? How would I implement it correctly? I also tried with a obj instanceof Y check but that doesn't work because of the way

内置的Symbol值

随声附和 提交于 2019-12-06 20:13:13
除了定义自己使用的 Symbol 值以外,ES6 还提供了 11 个内置的 Symbol 值,指向语言内部使用的方法。 对象的Symbol.hasInstance属性,指向一个内部方法。当其他对象使用instanceof运算符,判断是否为该对象的实例时,会调用这个方法。 比如, foo instanceof Foo 在语言内部,实际调用的是 Foo[Symbol.hasInstance](foo) 。 class MyClass { [Symbol.hasInstance](foo) { return foo instanceof Array; } } [1, 2, 3] instanceof new MyClass() // true 下面是另一个例子: 对象的 Symbol.isConcatSpreadable 属性等于一个布尔值,表示该对象用于 Array.prototype.concat() 时,是否可以展开。 let arr1 = ['c', 'd']; ['a', 'b'].concat(arr1, 'e') // ['a', 'b', 'c', 'd', 'e'] arr1[Symbol.isConcatSpreadable] // undefined let arr2 = ['c', 'd']; arr2[Symbol.isConcatSpreadable] =

JavaScriptES6内置的Symbol值

我们两清 提交于 2019-12-06 20:06:07
除了定义自己使用的Symbol值以外,ES6还提供了11个内置的Symbol值,指向语言内部使用的方法。 目录 Symbol.hasInstance Symbol.isConcatSpreadable Symbol.species Symbol.match Symbol.replace Symbol.search Symbol.split Symbol.iterator Symbol.toPrimitive Symbol.toStringTag Symbol.unscopables Symbol.hasInstance 对象的Symbol.hasInstance属性,指向内部方法,当其他对象使用instanceOf运算符,判断是否为该对象的实例时,会调用这个方法。 比如:foo instanceOf Foo 在语言内部,实例调用的是 Foo[Symbol.hasInstance](foo) 调用instanceof方法总是返回布尔值,不是布尔值隐式转换为布尔值。当return返回的数据为true的时候返回true,当return返回的数据为false的时候返回false,也就是说可以自定义修改规则 class MyClass{ [Symbol.hasInstance](foo) { let drag = foo instanceof Array ? false : true;

Java .equals() instanceof subclass? Why not call superclass equals instead of making it final?

只谈情不闲聊 提交于 2019-12-06 19:27:02
问题 It is stated in Object's .equals(Object) javadoc: It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. Almost everywhere in example code I see overridden .equals(Object) method which uses instanceof as one of the first tests, for example here: What issues / pitfalls must be considered when overriding equals and hashCode? public class Person { private String name; private int age; public boolean equals(Object obj) {

类型信息

↘锁芯ラ 提交于 2019-12-06 15:24:08
编译前 - 强制 运行时 - 反射 // typeinfo/Shapes.java import java.util.stream.*; abstract class Shape { void draw() { System.out.println(this + ".draw()"); } @Override public abstract String toString(); } class Circle extends Shape { @Override public String toString() { return "Circle"; } } class Square extends Shape { @Override public String toString() { return "Square"; } } class Triangle extends Shape { @Override public String toString() { return "Triangle"; } } public class Shapes { public static void main(String[] args) { Stream.of( new Circle(), new Square(), new Triangle()) .forEach(Shape::draw); }

Why does instanceof return false for some literals?

こ雲淡風輕ζ 提交于 2019-12-06 13:36:35
"foo" instanceof String //=> false "foo" instanceof Object //=> false true instanceof Boolean //=> false true instanceof Object //=> false false instanceof Boolean //=> false false instanceof Object //=> false // the tests against Object really don't make sense Array literals and Object literals match... [0,1] instanceof Array //=> true {0:1} instanceof Object //=> true Why don't all of them? Or, why don't they all not ? And, what are they an instance of, then? It's the same in FF3, IE7, Opera, and Chrome. So, at least it's consistent. Missed a few. 12.21 instanceof Number //=> false /foo/

Reflection: cast an object to subclass without use instanceof

回眸只為那壹抹淺笑 提交于 2019-12-06 04:43:24
问题 I have this simple interface/class: public abstract class Message {} public class Message1 extends Message {} public class Message2 extends Message {} And an utility class: public class Utility { public void handler(Message m) { System.out.println("Interface: Message"); } public void handler(Message1 m) { System.out.println("Class: Message1"); } public void handler(Message2 m) { System.out.println("Class: Message2"); } } Now, the main class: public static void main(String[] args) { Utility p