instanceof

How to test if one java class extends another at runtime?

依然范特西╮ 提交于 2019-11-26 09:16:53
问题 How to I test if a is a subclass of b ? Class<?> a = A.class; Class<?> b = B.class; 回答1: Are you looking for: Super.class.isAssignableFrom(Sub.class) 回答2: If you want to know whether or not a Class extends another, use Class#isAssignableFrom(Class). For your example, it would be: if(B.class.isAssignableFrom(A.class)) { ... } If you're interested in whether or not an instance is of a particular type, use instanceof : A obj = new A(); if(obj instanceof B) { ... } Note that these will return

Is there something like instanceOf(Class<?> c) in Java?

心不动则不痛 提交于 2019-11-26 07:35:58
问题 I want to check if an object o is an instance of the class C or of a subclass of C . For instance, if p is of class Point I want x.instanceOf(Point.class) to be true and also x.instanceOf(Object.class) to be true . I want it to work also for boxed primitive types. For instance, if x is an Integer then x.instanceOf(Integer.class) should be true . Is there such a thing? If not, how can I implement such a method? 回答1: Class.isInstance does what you want. if (Point.class.isInstance(someObj)){ ...

How to avoid &#39;instanceof&#39; when implementing factory design pattern?

你说的曾经没有我的故事 提交于 2019-11-26 05:57:59
问题 I am attempting to implement my first Factory Design Pattern, and I\'m not sure how to avoid using instanceof when adding the factory-made objects to lists. This is what I\'m trying to do: for (ABluePrint bp : bluePrints) { AVehicle v = AVehicleFactory.buildVehicle(bp); allVehicles.add(v); // Can I accomplish this without using \'instanceof\'? if (v instanceof ACar) { cars.add((ACar) v); } else if (v instanceof ABoat) { boats.add((ABoat) v); } else if (v instanceof APlane) { planes.add(

Java: Instanceof and Generics

北城余情 提交于 2019-11-26 05:57:34
问题 Before I look through my generic data structure for a value\'s index, I\'d like to see if it is even an instance of the type this has been parametrized to. But Eclipse complains when I do this: @Override public int indexOf(Object arg0) { if (!(arg0 instanceof E)) { return -1; } This is the error message: Cannot perform instanceof check against type parameter E. Use instead its erasure Object since generic type information will be erased at runtime What is the better way to do it? 回答1: The

What is the difference between typeof and instanceof and when should one be used vs. the other?

六月ゝ 毕业季﹏ 提交于 2019-11-26 04:29:00
问题 In my particular case: callback instanceof Function or typeof callback == \"function\" does it even matter, what\'s the difference? Additional Resource: JavaScript-Garden typeof vs instanceof 回答1: Use instanceof for custom types: var ClassFirst = function () {}; var ClassSecond = function () {}; var instance = new ClassFirst(); typeof instance; // object typeof instance == 'ClassFirst'; // false instance instanceof Object; // true instance instanceof ClassFirst; // true instance instanceof

Is it possible to use the instanceof operator in a switch statement?

…衆ロ難τιáo~ 提交于 2019-11-26 03:33:17
问题 I have a question of using switch case for instanceof object: For example: my problem can be reproduced in Java: if(this instanceof A) doA(); else if(this instanceof B) doB(); else if(this instanceof C) doC(): How would it be implemented using switch...case ? 回答1: This is a typical scenario where subtype polymorphism helps. Do the following interface I { void do(); } class A implements I { void do() { doA() } ... } class B implements I { void do() { doB() } ... } class C implements I { void

Avoiding instanceof in Java

纵然是瞬间 提交于 2019-11-26 03:17:36
Having a chain of "instanceof" operations is considered a "code smell". The standard answer is "use polymorphism". How would I do it in this case? There are a number of subclasses of a base class; none of them are under my control. An analogous situation would be with the Java classes Integer, Double, BigDecimal etc. if (obj instanceof Integer) {NumberStuff.handle((Integer)obj);} else if (obj instanceof BigDecimal) {BigDecimalStuff.handle((BigDecimal)obj);} else if (obj instanceof Double) {DoubleStuff.handle((Double)obj);} I do have control over NumberStuff and so on. I don't want to use many

What is the instanceof operator in JavaScript?

大憨熊 提交于 2019-11-26 02:26:58
问题 The instanceof keyword in JavaScript can be quite confusing when it is first encountered, as people tend to think that JavaScript is not an object-oriented programming language. What is it? What problems does it solve? When is it appropriate and when not? 回答1: instanceof The Left Hand Side (LHS) operand is the actual object being tested to the Right Hand Side (RHS) operand which is the actual constructor of a class. The basic definition is: Checks the current object and returns true if the

The performance impact of using instanceof in Java

心已入冬 提交于 2019-11-26 01:36:56
问题 I am working on an application and one design approach involves extremely heavy use of the instanceof operator. While I know that OO design generally tries to avoid using instanceof , that is a different story and this question is purely related to performance. I was wondering if there is any performance impact? Is is just as fast as == ? For example, I have a base class with 10 subclasses. In a single function that takes the base class, I do checks for if the class is an instance of the

Any reason to prefer getClass() over instanceof when generating .equals()?

拥有回忆 提交于 2019-11-26 01:35:17
问题 I\'m using Eclipse to generate .equals() and .hashCode() , and there is an option labeled \"Use \'instanceof\' to compare types\". The default is for this option to be unchecked and use .getClass() to compare types. Is there any reason I should prefer .getClass() over instanceof ? Without using instanceof : if (obj == null) return false; if (getClass() != obj.getClass()) return false; Using instanceof : if (obj == null) return false; if (!(obj instanceof MyClass)) return false; I usually