instanceof

What's the point of new String(“x”) in JavaScript?

你说的曾经没有我的故事 提交于 2019-11-26 13:49:25
问题 What are the use cases for doing new String("already a string") ? What's the whole point of it? 回答1: There's very little practical use for String objects as created by new String("foo") . The only advantage a String object has over a primitive string value is that as an object it can store properties: var str = "foo"; str.prop = "bar"; alert(str.prop); // undefined var str = new String("foo"); str.prop = "bar"; alert(str.prop); // "bar" If you're unsure of what values can be passed to your

How to see if an object is an array without using reflection?

可紊 提交于 2019-11-26 13:09:47
问题 How can I see in Java if an Object is an array without using reflection? And how can I iterate through all items without using reflection? I use Google GWT so I am not allowed to use reflection :( I would love to implement the following methods without using refelection: private boolean isArray(final Object obj) { //??.. } private String toString(final Object arrayObject) { //??.. } BTW: neither do I want to use JavaScript such that I can use it in non-GWT environments. 回答1: You can use Class

Is This Use of the “instanceof” Operator Considered Bad Design?

吃可爱长大的小学妹 提交于 2019-11-26 12:41:40
问题 In one of my projects, I have two \"data transfer objects\" RecordType1 and RecordType2 that inherit from an abstract class of RecordType. I want both RecordType objects to be processed by the same RecordProcessor class within a \"process\" method. My first thought was to create a generic process method that delegates to two specific process methods as follows: public RecordType process(RecordType record){ if (record instanceof RecordType1) return process((RecordType1) record); else if

Java 多态

拥有回忆 提交于 2019-11-26 12:31:52
目录 Java 多态 1. 多态的格式与使用 2. 多态中成员变量的访问特点 3. 多态中成员方法的使用特点 4. 多态的好处 5. 对象的向上转型 6. 对象的向下转型 7. 用instanceof关键字进行类型判断 8. 笔记本USB接口案例 Java 多态 一个对象拥有多种形态,这就是: 对象的多态性 。 1. 多态的格式与使用 代码当中体现多态性,其实就是一句话: 父类引用指向子类对象 。 格式: 父类名称 对象名 = new 子类名称(); 或者 接口名称 对象名 = new 实现类名称(); 2. 多态中成员变量的访问特点 和继承中一样,没有任何变化。 成员变量不能覆盖重写,成员方法能覆盖重写。 子类没有覆盖重写,就是父。 子类有覆盖重写,就是子。 3. 多态中成员方法的使用特点 口诀: 编译看左边,运行看右边 * 对比一下: 成员变量:编译看左边,运行还看左边。 成员方法:编译看左边,运行看右边。 4. 多态的好处 无论右边 new 的时候换成了哪个子类对象,等号左边的调用方法都不会变化。 5. 对象的向上转型 对象的向上转型,其实就是多态的写法。 格式: 父类名称 对象名 = new 子类名称(); 含义:右侧创建一个子类对象,把它当做父类来看待使用。 示例: Animal animal = new Cat();// 创建了一只猫,当作动物看,没问题 注意事项:

What is the instanceof operator in JavaScript?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 11:58: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? JonH 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 object is of the specified object type. Here are some good examples and here is an example taken directly

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

假装没事ソ 提交于 2019-11-26 11:38:26
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 ? 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 do() { doC() } ... } Then you can simply call do() on this . If you are not free to change A , B , and C ,

instanceof - incompatible conditional operand types

爷,独闯天下 提交于 2019-11-26 10:30:44
The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this doesn't: String s = new String(); System.out.println(s instanceof Cloneable); A compiler error is thrown. What is the problem? A more blatant incarnation of your problem is the following: if ("foo" instanceof Number) // "Incompatible conditional operand types String and Number" This is specified in JLS 15.20.2 Type comparison operator instanceof : RelationalExpression: RelationalExpression instanceof ReferenceType If a cast of the RelationalExpression to the ReferenceType would be

js实现instanceof

六眼飞鱼酱① 提交于 2019-11-26 10:28:50
instanceof 是通过原型链判断的,A instanceof B, 在A的原型链中层层查找,是否有原型等于B.prototype,如果一直找到A的原型链的顶端null,仍然不等于B.prototype,那么返回false,否则返回true. function instance(left,right){ left=left.__proto__ right=right.prototype while(true){ if(left==null) return false; if(left===right) return true; left=left.__proto__ } }    来源: https://www.cnblogs.com/wjgoblin/p/11317978.html

java关键字 instanceof

南笙酒味 提交于 2019-11-26 09:59:08
instanceof 严格来说是Java中的一个双目运算符,用来测试一个对象是否为一个类的实例,用法为: boolean result = obj instanceof Class   其中 obj 为一个对象,Class 表示一个类或者一个接口,当 obj 为 Class 的对象,或者是其直接或间接子类,或者是其接口的实现类,结果result 都返回 true,否则返回false   注意:编译器会检查 obj 是否能转换成右边的class类型,如果不能转换则直接报错,如果不能确定类型,则通过编译,具体看运行时定 1、obj 必须为引用类型,不能是基本类型 int i = 0; System.out.println(i instanceof Integer); //编译不通过 System.out.println(i instanceof Object); //编译不通过 instanceof 运算符只能用作对象的判断 2、obj 为 null System.out.println( null instanceof Object); //false 如果 obj 为 null,那么将返回 false 3、obj 为 class 类的实例对象 Integer integer = new Integer( 1 ); System.out.println(integer

How to check if a subclass is an instance of a class at runtime? [duplicate]

佐手、 提交于 2019-11-26 09:29:01
问题 This question already has answers here : Check if a Class Object is subclass of another Class Object in Java (7 answers) Closed 3 years ago . In an android app test suite I have a class like this where B is a view: public class A extends B { ... etc... } now I have a list of view objects which may contain A objects but in this case I only care if they\'re subclasses or \"instances of\" B . I\'d like to do something like: ArrayList<View> viewList = getViews(); Iterator<View> iterator =