[Java]instanceof和getClass()的区别
getClass() will be useful when you want to make sure your instance is NOT a subclass of the class you are comparing with.
?
class A { }class B extends A { }Object o1 = new A();Object o2 = new B();o1 instanceof A => trueo1 instanceof B => falseo2 instanceof A => true // <================ HEREo2 instanceof B => trueo1.getClass().equals(A.class) => trueo1.getClass().equals(B.class) => falseo2.getClass().equals(A.class) => false // <===============HEREo2.getClass().equals(B.class) => true?