instanceof

getClass()和instanceof以及类的equals方法

假装没事ソ 提交于 2019-12-03 01:59:35
在比较两个类时,常见有两种做法,一种是x.getClass() == y; 一种是x instanceof y,下面我们来比较这两种做法的区别。 getClass()返回一个对象所属的类 public static void main(String[] args) { Hero h1 = new Hero(null,10,2); Hero h2 = new Hero("zhang",10,2);     Superman s1 = new Superman("zhang"); System.out.println(h1.getClass());      System.out.println(h1.getClass() == h2.getClass());       System.out.println(h1.getClass() == s1.getClass());       System.out.println(s1.getClass() == h1.getClass()); }返回:  class source.Hero   true   false   false 可以看到, getClass返回的是一个类名,也就是说只会在类名相同时返回true,不会判断子类与父类的继承关系。 instanceof比较一个对象是否是该类的实例 public static void

Can I catch multiple Java exceptions in the same catch clause?

匿名 (未验证) 提交于 2019-12-03 01:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In Java, I want to do something like this: try { ... } catch (IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException e) { someCode(); } ...instead of: try { ... } catch (IllegalArgumentException e) { someCode(); } catch (SecurityException e) { someCode(); } catch (IllegalAccessException e) { someCode(); } catch (NoSuchFieldException e) { someCode(); } Is there any way to do this? 回答1: This is possible since Java 7 . The syntax for try-catch block is: try { ... } catch (IOException | SQLException ex) { ... }

javax.el.ELException: Failed to parse the expression [{pz:instanceof(object,'com.project.domain.MyClass')}]

匿名 (未验证) 提交于 2019-12-03 01:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Currenty I have a web project with JSF 1.2 and Facelets running in tomcat 6.0.18.0. I decided to upgrade the servlet container, thus i deployed in tomcat 7 and all seemed ok until we hit one view using my custome facelet functions. javax.el.ELException: Failed to parse the expression [{pz:instanceof(object,'com.project.domain.MyClass')}] Caused by: org.apache.el.parser.ParseException: Encountered " ":" ": "" at line 1, column 5. Was expecting one of: "}" ... "." ... "[" ... This error occurs when parsing the following code: ... If i

Cannot perform instanceof check against parameterized type ArrayList<Foo>

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The following code: ((tempVar instanceof ArrayList ) ? tempVar : null); causes: Cannot perform instanceof check against parameterized type ArrayList . Use the form ArrayList> instead since further generic type information will be erased at runtime Can someone explain me what is meant by "further generic type information will be erased at runtime" and how to fix this? 回答1: It means that if you have anything that is parameterized, e.g. List fooList = new ArrayList (); , the Generics information will be erased at runtime. Instead, this is what

Object.isArray() is slow, is there a fast way to do that?

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In my application obj.getClass().isArray() is called very frequently and become the bottleneck of the app. I want to check efficiently at run-time if an object is an array. Primitive array and object array should return true. The way I can imagine is to instanceof all primtive arrays, but cannot handle types like int[][]. And the app is used as lib, so I cannot list all types. Is there any clue for that? 回答1: A benchmark I've just done gave the following results: {s instanceof Object[]} spends 44ms {s.getClass().getName().charAt(0) == '['}

js里typeof和instanceof要注意的地方

孤街浪徒 提交于 2019-12-03 01:26:42
如果学过类似C#这样的语言,然后定义两个类class Mu{}和class Ku{},那么显然typeof Mu != typeof Ku的,但是在js里则不是这样,对于Mu和Ku的对象进行typeof后它们输出都是object,而对typeof Mu这个类名则输出function,即类名其实是一个函数类型,所以可以这样调用Mu(),也可以new Mu(),这个和es5倒是一致的; 如果直接输出Mu则得到function Mu()这样一个字符串; 综上,js的typeof不能很好的支持判断自定义类型(js里类型就只有固定的几种,String,Object,Symbol。。。); 而instanceof则比较符合人的直觉,它能判断出对象是否是某个自定义类型的实例; 如,虽然typeof mu === typeof ku,但是mu instanceof Ku则是返回false; 来源: https://www.cnblogs.com/silentdoer/p/11769181.html

Why is Object.prototype instanceof Object false?

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Why does the following return false? Object.prototype instanceof Object 回答1: Because it basically asks whether Object.prototype does inherit from Object 's .prototype object: It does not. a instanceof b is equivalent to b.prototype.isPrototypeOf(a) - it tests whether b.prototype is in the prototype chain of a . In your case, it is not in the chain, because it is the start of the chain itself. isPrototypeOf is not reflexive. 回答2: Referencing MDN : The instanceof operator tests whether an object has in its prototype chain the prototype

JSqlParser - Pretty print where clause

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have started to use JSqlParser, i can parse a Where clause but i don't manage to go further with it. JSqlParser github link Indeed, i have tried to Override visit methods but don't understand how to reach my goal. Let's say i have: (a=1 AND (b=2 OR (c=3 AND d=4))) OR e=2 as input and i would like as output: -> a=1 -> AND --> b=2 --> OR ---> c=3 ---> AND ---> d=4 -> OR -> e=5 My final goal is not a pretty print, but to understand how to know the current depth of the print at a given state. In order to dig in the tree as i want. I have

详解数据类型检测的四种方式

匿名 (未验证) 提交于 2019-12-03 00:41:02
Ŀ¼ 详解数据类型检测的四种方式 typeof:用来检测数据类型的运算符 instanceof:检测某一个实例是否属于某个类 constructor:构造函数 注意:对于特殊的数据类型,比如:null和undefined,他们的所属类是Null和Undefined,但是浏览器把这两个类保护起来了,不允许在外面访问使用 Object.prototype.toString.call():最准确最常用的方式 对toString的理解 console.log(typeof 12); var name = ‘xiaoke‘; console.log(typeof name); 使用typeof检测数据类型,首先返回的都是一个字符串,其次字符串中包含了对应的数据类型 例如:"number"、"string"、"boolean"、"undefined"、"function"、"object"、"" 局限性: typeof null->"object" 不能具体细分是数组还是正则,还是对象中其他的值,因为使用type哦对检测数据类型,对于对象数据类型中的值,最后返回的结果都是"object" console.log(typeof typeof typeoof function());//->"string" function fn(callback){ //typeof callback ===

遍历找到layout中的某种控件

匿名 (未验证) 提交于 2019-12-03 00:34:01
private void findEditText ( ViewGroup group ) { if ( group != null ) { for ( int i = 0 , j = group . getChildCount (); i < j ; i ++) { View child = group . getChildAt ( i ); if ( child instanceof EditText ) { EditText child1 = ( EditText ) child ; if ( isNeed ( child1 . getText (). toString ())) //根据需求判断是否添加 continue ; edList . add ( child1 ); } else if ( child instanceof ViewGroup ) { findEditText (( ViewGroup ) child ); } } } } 转载请标明出处: 遍历找到layout中的某种控件 文章来源: 遍历找到layout中的某种控件