instanceof

Why does `instanceof` error rather than return `false` when used for 2 incompatible classes?

前提是你 提交于 2019-11-27 07:45:50
问题 I'm reading this: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2 They say: Consider the example program: class Point { int x, y; } class Element { int atomicNumber; } class Test { public static void main(String[] args) { Point p = new Point(); Element e = new Element(); if (e instanceof Point) { // compile-time error System.out.println("I get your point!"); p = (Point)e; // compile-time error } } } The instanceof expression is incorrect because no instance of

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

自闭症网瘾萝莉.ら 提交于 2019-11-27 07:31:21
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. You can use Class.isArray() public static boolean isArray(Object obj) { return obj!=null && obj.getClass().isArray(); } This

js学习之面向对象

拟墨画扇 提交于 2019-11-27 06:10:17
一、创建对象的方法 1、 {} 字面量创建 var person ={ name: "lisi", age: 21, say: function(){ alert(this.name); } }; 2、 new Object() var person = new Object(); person.name = "lisi"; person.age = 21; person.family = ["lida","lier","wangwu"]; person.say = function(){ alert(this.name); } 3: 用构造函数创建 function Person(name,age,family) { this.name = name; this.age = age; this.family = family; this.say = function(){ alert(this.name); } } var person1 = new Person("lisi",21,["lida","lier","wangwu"]); var person2 = new Person("lisi",21,["lida","lier","lisi"]); console.log(person1 instanceof Object); //true console.log

Switch over type in java

时光怂恿深爱的人放手 提交于 2019-11-27 05:25:04
问题 Before I start, I know there are a bunch of answers to this question that suggest alternate approaches. I'm looking for assistance to this particular approach as to whether it is possible, and if not, similar approaches that might work. I have a method that takes a superclass and calls a method based on the type of the passed object. for instance: public void handle(Object o){ if (o instanceof A) handleA((A)o); else if (o instanceof B) handleB((B)o); else if (o instanceof C) handleC((C)o);

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

杀马特。学长 韩版系。学妹 提交于 2019-11-27 03:57:36
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 (record instanceof RecordType2) return process((RecordType2) record); throw new IllegalArgumentException(record);

Java运算符

六月ゝ 毕业季﹏ 提交于 2019-11-27 02:45:18
运算符 自增和自减运算符 ++ :操作数值型变量,++ 放操作数左边,操作数自身加 1,返回加 1 后的结果;++ 放操作数右边,操作数自身加 1, 返回加 1 前的结果; -- :操作数值型变量,-- 放操作数左边,操作数自身减 1,返回减 1 后的结果;-- 放操作数右边,操作数自身减 1, 返回减 1 前的结果; int c = 5; int d = ++c; System.out.println(a); //6 System.out.println(d); //6 int a = 5; int b = a++; System.out.println(a); //6 System.out.println(b); //5 位运算符 & :按位与 | :按位或 ~ :按位非(键盘数字 1 左边键) ^ :按位异或 << :左移运算符 >> :右移运算符 >>> :无符号右移运算符 需要记住一点, 位运算操作的是数值在计算机存储的二进制码(补码) System.out.println(5 & 9); //1 System.out.println(5 | 9); //13 System.out.println(~-5); //4 System.out.println(5^9); //12 左移运算符是 将操作数的二进制码整体左移指定位数,左移后右边空出来的位以 0 填充 ,左移 n

How to know if an object is an instance of a TypeTag's type?

六月ゝ 毕业季﹏ 提交于 2019-11-27 02:36:34
问题 I have a function which is able to know if an object is an instance of a Manifest 's type. I would like to migrate it to a TypeTag version. The old function is the following one: def myIsInstanceOf[T: Manifest](that: Any) = implicitly[Manifest[T]].erasure.isInstance(that) I have been experimenting with the TypeTags and now I have this TypeTag version: // Involved definitions def myInstanceToTpe[T: TypeTag](x: T) = typeOf[T] def myIsInstanceOf[T: TypeTag, U: TypeTag](tag: TypeTag[T], that: U)

到底instanceof是啥?

耗尽温柔 提交于 2019-11-27 00:50:58
对Js有一定了解的盆友肯定都知道instanceof 并且还很常用,比如说用 [1, 2, 3] instanceof Array 来判断是否是数组。所以我们可能会简单的以为他就是一个用来判断typeof无法判断的复杂数据类型的。但是,有些时候instaneof似乎并不是我们想象的那么简单。比如: 1 123 instanceof Number 2 new Number(123) instanceof Number 3 Number(123) instanceof Number 思考一下上面的三个表达式返回什么呢?(在心里默默地思考一下你的答案,会不会是 true false true呢? 哈哈,那你还真就掉坑里了呢~) 咦~为什么是这样勒?(没有这种疑问的童鞋我的这篇博客可能满足不了你了~嘻嘻) 事实上,instanceof的使用定义为 The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor. 意思也就是说instanceof这个运算符是用来测试一个对象的原型链上是否有该原型的构造函数,即instanceof左表达式要是一个对象,右侧表达式要是一个构造函数,并且左侧是右侧实例化出来的对象才会返回true

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

一曲冷凌霜 提交于 2019-11-27 00:47:59
This question already has an answer here: Check if a Class Object is subclass of another Class Object in Java 7 answers 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 = viewList.iterator(); while (iterator.hasNext() && viewList != null) { View view = iterator.next(); if (view.getClass().isInstance(B