java中instanceof與Class的等價(jià)性代碼示例
本文研究的主要是java中instanceof與Class的等價(jià)性的相關(guān)問題,具體如下。
java 中的instanceof 運(yùn)算符是用來在運(yùn)行時(shí)指出對象是否是特定類的一個(gè)實(shí)例。instanceof通過返回一個(gè)布爾值來指出,這個(gè)對象是否是這個(gè)特定類或者是它的子類的一個(gè)實(shí)例。
實(shí)例1(instanceof)
接口Person
public interface Person {
public void eat();
}
實(shí)現(xiàn)類People
public class People implements Person {
private int a=0;
@Override
public void eat() {
System.out.println("======"+a);
}
}
子類xiaoming:
public class xiaoming extends People {
private String name;
@Override
public void eat() {
System.out.println("+++++++++");
}
}
主函數(shù)
public static void main(String[] args) {
People p=new People();
xiaoming x=new xiaoming();
System.out.println(p instanceof Person);
System.out.println(p instanceof xiaoming); -----2
System.out.println(x instanceof Person);
System.out.println(x instanceof People);
}
注意:上面2處的代碼在編譯時(shí)不會報(bào)錯(cuò)。
運(yùn)行結(jié)果:
true false true true
實(shí)例2
package com.test.class_obj;
class Base {
}
class Derived extends Base {
}
public class FamilyVsExactType {
static void test(Object x) {
System.out.println("Testing x of type " + x.getClass().getSimpleName());
System.out.println("-----------------------------------------");
System.out.println("x instanceof Base " + (x instanceof Base));
System.out.println("x instanceof Derived " + (x instanceof Derived));
System.out.println("-----------------------------------------");
System.out.println("Base.isInstance(x) " + Base.class.isInstance(x));
System.out.println("Derived.isInstance(x) " +
Derived.class.isInstance(x));
System.out.println("-----------------------------------------");
System.out.println("x.getClass() == Base.class " +
(x.getClass() == Base.class));
System.out.println("x.getClass() == Derived.class " +
(x.getClass() == Derived.class));
System.out.println("x.getClass().equals(Base.class)) " +
(x.getClass().equals(Base.class)));
System.out.println("x.getClass().equals(Derived.class)) " +
(x.getClass().equals(Derived.class)));
System.out.println("*****************************************");
System.out.println("*****************************************");
}
public static void main(String[] args) {
test(new Base());
test(new Derived());
}
}
輸出內(nèi)容如下:
Testing x of type Base ----------------------------------------- x instanceof Base true x instanceof Derived false ----------------------------------------- Base.isInstance(x) true Derived.isInstance(x) false ----------------------------------------- x.getClass() == Base.class true x.getClass() == Derived.class false x.getClass().equals(Base.class)) true x.getClass().equals(Derived.class)) false ***************************************** ***************************************** Testing x of type Derived ----------------------------------------- x instanceof Base true x instanceof Derived true ----------------------------------------- Base.isInstance(x) true Derived.isInstance(x) true ----------------------------------------- x.getClass() == Base.class false x.getClass() == Derived.class true x.getClass().equals(Base.class)) false x.getClass().equals(Derived.class)) true ***************************************** ***************************************** Process finished with exit code 0
通過以上測試可以得出以下結(jié)論:
- instanceof() 和 isInstance() 生成的結(jié)果相同
- equals() 和 == 生成的結(jié)果相同
- 父類可以是子類的實(shí)例,但子類不可以是父類的實(shí)例
- Class 對象比較時(shí),不考慮繼承
總結(jié)
以上就是本文關(guān)于java中instanceof與Class的等價(jià)性代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
SpringBoot項(xiàng)目如何訪問jsp頁面的示例代碼
本篇文章主要介紹了SpringBoot項(xiàng)目如何訪問jsp頁面的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
dubbo新手學(xué)習(xí)之事件通知實(shí)踐教程
這篇文章主要給大家介紹了關(guān)于dubbo新手學(xué)習(xí)之事件通知實(shí)踐的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java中main函數(shù)的String[]?args用法舉例詳解
這篇文章主要給大家介紹了關(guān)于Java中main函數(shù)的String[]?args用法的相關(guān)資料,JAVA類中main函數(shù)的參數(shù)String[]?args指的是運(yùn)行時(shí)給main函數(shù)傳遞的參數(shù),文中通過圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Spring Native 基礎(chǔ)環(huán)境搭建過程
Spring?Native可以通過GraalVM將Spring應(yīng)用程序編譯成原生鏡像,提供了一種新的方式來部署Spring應(yīng)用,本文介紹Spring?Native基礎(chǔ)環(huán)境搭建,感興趣的朋友跟隨小編一起看看吧2024-02-02
詳解json在SpringBoot中的格式轉(zhuǎn)換
這篇文章主要介紹了詳解json在SpringBoot中的格式轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

