Class类中isInstance的使用
如果此Class对象表示一个数组类,如果指定的Object参数可以通过恒等转换或扩展引用转换转换为数组类的对象,则此方法返回true ;否则返回false 。
除了object是类的对象外,还有什么情况是true;
//如果object是数组对象
Integer[] integers = new Integer[0];
System.out.println(Number.class.isInstance(integers)); //false
System.out.println(new Integer[0].getClass().isInstance(integers)); //true
子类对象也可以,就是在解释说明中的可转换的情况,如下情况均为true。
Father father = new Son();
System.out.println(father instanceof Father);
System.out.println(father instanceof Son);
Son son = new Son();
System.out.println(son instanceof Father);
System.out.println(son instanceof Son);
Father[] fathers = new Son[0];
System.out.println(new Father[0].getClass().isInstance(fathers));
System.out.println(new Son[0].getClass().isInstance(fathers));
Son[] sons = new Son[0];
System.out.println(new Father[0].getClass().isInstance(sons));
System.out.println(new Son[0].getClass().isInstance(sons));
Father为Son的父类,这个是判断实例的类型,所以主要看等号右边。
有帮助请采纳,还有不懂的可以继续追问~