Type和class以及比较是相等关系
前言
Type是Class的父接口。
Class是Type的子类。
instanceof
instanceof操作符用于判断一个引用类型所引用的对象是否是一个类的实例。
public void instanceOf(Object object) {
/**
* ==
*/
if(Long.class == object.getClass()){
System.out.println("Long.class == object.getClass()");
}
/**
* equals
*/
if(Long.class.equals(object.getClass()) ){
System.out.println("Long.class.equals(object.getClass()) ");
}
/**
* instance Of
*/
if(object instanceof Long){
System.out.println("object instanceof Long");
}
}
== 和 equals比较的实际的Class类,没有考虑继承,instanceof 和 isInstance考虑继承
class Father{}
class Son extends Father{}
public void fatherAndSon(){
Father son = new Son(); //和 Son son = new Son(); 一样
Father father = new Father();
//son.getClass() == father.getClass() :false
System.out.println("son.getClass() == father.getClass() :" +(son.getClass() == father.getClass()) );
//son.getClass().equals(father.getClass()) :false
System.out.println("son.getClass().equals(father.getClass()) :" +(son.getClass().equals(father.getClass())) );
//son instanceof Father :true
System.out.println("son instanceof Father :" +(son instanceof Father ));
//father instanceof Son :false //父类不能 instanceof 子类
System.out.println("father instanceof Son :" +(father instanceof Son ));
//father instanceof Father :true
System.out.println("father instanceof Father :" +(father instanceof Father ));
//son.getClass().isInstance(father):false //子类不能 isInstance 父亲
System.out.println("son.getClass().isInstance(father):"+son.getClass().isInstance(father));
//father.getClass().isInstance(son):true
System.out.println("father.getClass().isInstance(son):"+father.getClass().isInstance(son));
//son.getClass().isInstance(son)true
System.out.println("son.getClass().isInstance(son)"+son.getClass().isInstance(son));
}
@Test
public void testFatherAndSon(){
fatherAndSon();
}
感兴趣的,欢迎添加博主微信,
哈,博主很乐意和各路好友交流,如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,备注您的微信或者其他联系方式。添加博主微信哦。
请下方留言吧。可与博主自由讨论哦
微信 | 微信公众号 | 支付宝 |
---|---|---|