instanceof

instanceof和getClass的区别

僤鯓⒐⒋嵵緔 提交于 2019-11-28 00:08:35
instanceof对比getClass:   instanceof 比较的是继承关系或者实现关系的类类型,子类对象或者实现类对象放在前面;而getClass得到的是确切的类型,并不考虑继承,它判断的是引用指向的对象的类型,与声明该变量的类型无关   继承/接口关系:父类和子类的关系,包括从子类到父类的抽象过程,从父类到子类的扩展过程 //父类接口 public interface Fruit { } //子类实现 public class Apple implements Fruit{ } public class Test { public static void main(String[] args) { Class clazz = Apple.class; Fruit o = new Apple(); if(o instanceof Apple){ System.out.println("o instanceof Apple"); //会输出 } if(o instanceof Fruit){ System.out.println("o instanceof Fruit");//会输出 } if(o.getClass()==Apple.class){ System.out.println("o.getClass()==Apple.class");//会输出 } if(o

java关键字之instanceof

梦想与她 提交于 2019-11-28 00:08:06
首先来看段测试代码 public class TestInstanceof{ public static void main(String[] args){ int a = 1; if(a instanceof String){ System.out.println("a instanceof String"); } }} 对这段代码进行编译,编译器首先会将源代码中的字符转换为Token(com.sun.tools.javac.parser.Token) 序列, 我们关注的是关键字instanceof ,它会被映射到一个Token.INSTANCEOF的token. 转换为Token序列这个过程主要是JavacParser结合Scanner类完成。 接着会尝试生成语法树节点,我们关注的代码 " a instanceof String "会生成JCTree.JCInstanceOf这个节点. 接下来进行语义分析,主要的过程在com.sun.tools.javac.comp.Attr.attribClassBody这个方法中。在这个方法中,会对上面的JCTree.JCInstanceOf这个节点进行类型检查,见下图 在第一行的方法中,首先获取变量a所对应的Type,最终发现a是一个int类型的Type,然后进入下面的check int类型的Type,其tag为4

Does instanceof return true if instance of a parent?

孤街醉人 提交于 2019-11-27 22:54:36
问题 I have a class Child that extends Parent . Parent child = new Child(); if (child instanceof Parent){ // Do something } Does this returns true or false, and why? 回答1: Yes, it would. And why should it not? Because child is in fact an instance of Parent. If, you want to perform an operation only for a child you should check if (child instanceof Child){ } However you should remember the following statement from Effective C++, by Scott Meyers : "Anytime you find yourself writing code of the form

Why cast after an instanceOf?

孤街醉人 提交于 2019-11-27 22:45:59
In the example below (from my coursepack), we want to give to the Square instance c1 the reference of some other object p1 , but only if those 2 are of compatible types. if (p1 instanceof Square) {c1 = (Square) p1;} What I don't understand here is that we first check that p1 is indeed a Square , and then we still cast it. If it's a Square , why cast? I suspect the answer lies in the distinction between apparent and actual types, but I'm confused nonetheless... Edit: How would the compiler deal with: if (p1 instanceof Square) {c1 = p1;} Edit2: Is the issue that instanceof checks for the actual

Objective c isKindOfClass missunderstanding?

假装没事ソ 提交于 2019-11-27 21:18:40
I have following structure of objects: Animal, Dog and Cat. As You expect Dog and Cat are inherited from Animal. And I've a farm class: @implementation AnimalFarm -(Animal*) createAnimal:(AnimalType)type{ switch (type) { case CAT: return [Cat new]; case DOG: return [Dog new]; default: return [Animal new]; } } @end and I tried to unit test: AnimalFarm *farm = [AnimalFarm new]; Animal *dog = [farm createAnimal:DOG]; Animal *cat = [farm createAnimal:CAT]; STAssertTrue([cat isMemberOfClass:[Cat class]],@"cat is not a cat!"); STAssertTrue([dog isMemberOfClass:[Dog class]],@"Dog is not a dog!");

js中instanceof的原理

寵の児 提交于 2019-11-27 20:47:31
// 思路:右边变量的原型存在于左边变量的原型链上 function instanceOf(left, right) { let leftValue = left.__proto__ let rightValue = right.prototype while (true) { if (leftValue === null) { return false } if (leftValue === rightValue) { return true } leftValue = leftValue.__proto__ } } 来源: https://blog.csdn.net/weixin_43837268/article/details/99687112

Why does 'instanceof' in TypeScript give me the error “'Foo' only refers to a type, but is being used as a value here.”?

ε祈祈猫儿з 提交于 2019-11-27 19:14:36
I wrote this code interface Foo { abcdef: number; } let x: Foo | string; if (x instanceof Foo) { // ... } But TypeScript gave me this error: 'Foo' only refers to a type, but is being used as a value here. Why is this happening? I thought that instanceof could check whether my value has a given type, but TypeScript seems not to like this. What's going on The issue is that instanceof is a construct from JavaScript, and in JavaScript, instanceof expects a value for the right-side operand. Specifically, in x instanceof Foo JavaScript will perform a runtime check to see whether Foo.prototype exists

The 'instanceof' operator behaves differently for interfaces and classes

ⅰ亾dé卋堺 提交于 2019-11-27 19:12:39
I would like to know regarding following behavior of instanceof operator in Java. interface C {} class B {} public class A { public static void main(String args[]) { B obj = new B(); System.out.println(obj instanceof A); //Gives compiler error System.out.println(obj instanceof C); //Gives false as output } } Why is it so? There is no relation between interface C and class B , but it gives false whereas in case of obj instanceof A it gives compiler error? Because Java has no multiple class inheritance it's absolutely known during the compilation that obj object of type B cannot be subtype of A

Objective c isKindOfClass missunderstanding?

試著忘記壹切 提交于 2019-11-27 19:12:38
问题 I have following structure of objects: Animal, Dog and Cat. As You expect Dog and Cat are inherited from Animal. And I've a farm class: @implementation AnimalFarm -(Animal*) createAnimal:(AnimalType)type{ switch (type) { case CAT: return [Cat new]; case DOG: return [Dog new]; default: return [Animal new]; } } @end and I tried to unit test: AnimalFarm *farm = [AnimalFarm new]; Animal *dog = [farm createAnimal:DOG]; Animal *cat = [farm createAnimal:CAT]; STAssertTrue([cat isMemberOfClass:[Cat

typeof for RegExp

孤者浪人 提交于 2019-11-27 19:12:24
Is there anyway to detect if a JavaScript object is a regex? For example, I would like to do something like this: var t = /^foo(bar)?$/i; alert(typeof t); //I want this to return "regexp" Is this possible? Thanks! EDIT: Thanks for all the answers. It seems I have two very good choices: obj.constructor.name === "RegExp" or obj instanceof RegExp Any major pros/cons to either method? Thanks again! Cleiton You can use instanceof operator: var t = /^foo(bar)?$/i; alert(t instanceof RegExp);//returns true In fact, that is almost the same as: var t = /^foo(bar)?$/i; alert(t.constructor == RegExp);/