首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

[Java]instanceof跟getClass()的区别

2012-12-25 
[Java]instanceof和getClass()的区别getClass() will be useful when you want to make sure your instanc

[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
?

热点排行