instanceof

Does “instanceof Void” always return false?

亡梦爱人 提交于 2019-11-29 16:21:55
问题 Can this method return true somehow? public static <T> boolean isVoid(T t) { return t instanceof Void; } 回答1: Yes, but I'm sure that isn't really useful: public static void main(final String[] args) throws Exception { final Constructor c = Void.class.getDeclaredConstructors()[0]; c.setAccessible(true); System.out.println(c.newInstance(null) instanceof Void); } A Void class can't be instantiated so normally your code wouldn't require to deal with Void instances. The above code snippet is just

Passing a class as an argument to a method in java

狂风中的少年 提交于 2019-11-29 13:21:45
I am writing a method were I would like to pass a class to a method, where a part of the code includes checking if the object is of a certain type. This is what I want (but which obviously doesn't work): private static class MyClass1 { /***/ } private static class MyClass2 { /***/ } private void someFunc() { /* some code */ methodName(MyClass1); methodName(MyClass2); } private void methodName(Class myClass) { Object obj; /* Complicated code to find obj in datastructure */ if (obj instanceof myClass) { /* Do stuff */ } } Any hints as to how this can be done? Thanks! Class has both an isInstance

PlayJava Day009

强颜欢笑 提交于 2019-11-29 12:24:11
今日所学: /* 2019.08.19开始学习,此为补档。 */ 1.Date工具类: Date date = new Date() ; //当前时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ; sdf.format(date) ----> 时间转字符串 sdf.parse(source) ----> 字符串转时间 2.Object类:是所有类的父类。 打开类的所有信息:ctrl+o public String toString() //返回该对象的字符串表示 return this.getName() ; //重写 public boolean equals (Object obj) { String name = ((People)obj).getName() ; return this.name == name ; //重写 3.instanceof关键字:判断一个对象是否属于一个类。 格式:对象 instanceof 类 ----> 返回布尔类型 if (animal instanceof Dog) { ((Dog)animal).func1() ; } if (animal instanceof Cat) { ((Cat)animal).func2() ; } //即用于向下转型作判断 4

Java 运算符

心不动则不痛 提交于 2019-11-29 12:18:42
本人博客园链接 https://www.cnblogs.com/zongmin/p/11339019.html 运算符 自增和自减运算符 ++ :操作数值型变量,++ 放操作数左边,操作数自身加 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

How to find type without using instanceof?

◇◆丶佛笑我妖孽 提交于 2019-11-29 11:30:22
I have a List of interface type Criteria within my class Query . List<Criteria> criteria = new ArrayList<Criteria>(); I have several concrete implementations of Criteria . I want to give Query a method that iterates through my criteria list, and depending on the concrete type, execute some logic. I'm presently doing this with instanceof like so: for(Criteria c : criteria) { if(c instanceof ContextualCriteria){ // logic } ... } Is this the only/best way? Does the logic sensibly belong in the Criteria itself? If so, put it into the Criteria interface and implement it appropriately for each

怎样理解 instanceof

て烟熏妆下的殇ゞ 提交于 2019-11-29 09:45:56
instanceof 运算符用来判断一个对象在其原型链中是否存在一个构造函数的 prototype 属性。 也就是说, instanceof 判断的实际上是某个对象是否为某个构造函数的实例, 因为 es5 中没有类的概念, 这里的 instanceof 其实是充当了一个判断类的实例对象的功能. 比如下面的两个例子: document.getElementsByTagName() 返回一个类似数组的对象, 这个对象是 HTMLCollection 这个构造函数的实例. 而 document.querySelectorAll() 返回一个类似数组的对象, 这个对象是 NodeList 这个构造函数的实例. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <ul> <li>李雷</li> <li>韩梅梅</li> <li>李强</li> <li>徐帆</li> </ul> <script>

Instanceof and namespaces

五迷三道 提交于 2019-11-29 09:27:33
I am facing an unexpected behaviour trying to use the following: $object instanceof $class 1/ PHP 'instanceof' keyword and namespaces work well together, as explained in the official doc. 2/ Sometimes, however, backslash escaping gives in to more subtle (obscure?) behaviour, as Ben kindly explained in this nice post. Somewhere deep down in my code, y set a couple of dumps as follow: var_dump($object, $class); var_dump($object instanceof $class); which gives me the following output when running my script: class Tools\Tests\Entity\testObject#226 (2) { private $var_one => NULL private $var_two =>

Does instanceof return true if instance of a parent?

早过忘川 提交于 2019-11-29 05:24:08
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? Ozair Kafray 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 "if the object is of type T1, then do something, but if it's of type T2, then do something else

php 中 instanceof 操作符

非 Y 不嫁゛ 提交于 2019-11-29 03:29:25
"instanceof"操作符的使用非常简单,它用两个参数来完成其功能。 第一个参数是你想要检查的对象,第二个参数是类名(事实上是一个接口名),用于确定是否这个对象是相应类的一个实例。它的基本语法如下: if (object instanceof class name){//继承关系 // } 作用:(1)判断一个对象是否是某个类的实例,(2)判断一个对象是否实现了某个接口。 第一种用法: 1 <?php 2 class A 3 { 4 public function index() 5 { 6 // 7 } 8 } 9 10 $obj = new A(); 11 if ($obj instanceof A) { 12 echo 'A'; 13 } else { 14 echo 'no A'; 15 } 16 17 输出结果:A 第二种用法: 1 <?php 2 interface ExampleInterface 3 { 4 public function interfaceMethod(); 5 } 6 7 class ExampleClass implements ExampleInterface 8 { 9 public function interfaceMethod() 10 { 11 return 'Hello World!'; 12 } 13 } 14 15

How can I reduce the Cyclomatic Complexity of this?

*爱你&永不变心* 提交于 2019-11-29 03:20:31
I have a method that receives an Object and does something based on what type of object it detects: void receive(Object object) { if (object instanceof ObjectTypeA) { doSomethingA(); } else { if (object instanceof ObjectTypeB) { doSomethingB(); } else { if (object instanceof ObjectTypeC) { doSomethingC(); } else { if (object instanceof ObjectTypeD) { doSomethingD(); } else { // etc... } } } } } How can I reduce the Cyclomatic Complexity? I searched around but couldn't find anything too useful. Can't you leverage an object-oriented approach for this? Create an interface that has the doSomething