java-----instanceof与getClass的区别
在比较一个类是否和另一个类属于同一个类实例的时候,我们通常可以采用instanceof和getClass两种方法通过两者是否相等来判断,但是两者在判断上面是有差别的,下面从代码中看看区别:
public class Test
{
public static void testInstanceof(Object x)
{
System.out.println("x instanceof Parent: "+(x instanceof Parent));
System.out.println("x instanceof Child: "+(x instanceof Child));
System.out.println("x getClass Parent: "+(x.getClass() == Parent.class));
System.out.println("x getClass Child: "+(x.getClass() == Child.class));
}
public static void main(String[] args) {
testInstanceof(new Parent());
System.out.println("---------------------------");
testInstanceof(new Child());
}
}
class Parent {
}
class Child extends Parent {
}
/*
输出:
x instanceof Parent: true
x instanceof Child: false
x getClass Parent: true
x getClass Child: false
---------------------------
x instanceof Parent: true
x instanceof Child: true
x getClass Parent: false
x getClass Child: true
*/
从程序输出可以看出,instanceof进行类型检查规则是:你属于该类吗?或者你属于该类的派生类吗?而通过getClass获得类型信息采用==来进行检查是否相等的操作是严格的判断。不会存在继承方面的考虑;
instanceof 与 getClass
1 instanceof
子类对象 Instanceof 父类class,为true
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof WimComplexProperty)) {
return false;
}
JavaBean other = (JavaBean) obj;
return Objects.equals(this.getId(), other.getId());
2 getClass
子类对象 obj.getClass() == 父类class,为false
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
JavaBean other = (JavaBean) obj;
return Objects.equals(this.getId(), other.getId());
标签:instanceof,getClass,Parent,-----,Child,obj,true
From: https://www.cnblogs.com/sunupo/p/18414337