Java學習筆記15---instanceof與向下轉型


感冒咳嗽停更了幾天,今天恢復更新了。

先來看下instanceof與向下轉型的概念:

1.instanceof

    instanceof是一個二元操作符,用法是:boolean result = a instanceof ClassA,即判斷對象a是否是類ClassA的實例,如果是的話,則返回true,否則返回false。

 

2.向下轉型

    對於向上轉型,筆記12已經說明過了,即父類對象引用指向了子類對象;向下轉型是指,子類的對象引用被賦值為父類對象引用,並且賦值時要進行顯式的強制類型轉換,而該父類對象引用必須是指向子類對象的;向下轉型最終還是子類對象引用指向了子類對象,也就是說,向上轉型后,再通過向下轉型變回來;向上轉型后丟失的屬性和方法經過向下轉型又可見了。關於向下轉型的意義,請參考http://blog.csdn.net/xyh269/article/details/52231944。

    用文字描述起來比較繞口,看下面的式子就好理解了。記Person為父類,Student為子類。

    Person per = new Student(); //向上轉型

    Student stu = (Student) per; //向下轉型

    由於per本來指向的就是子類對象,所以可以通過顯式的類型轉換使stu也指向該子類對象。

 

    對於下面的語句,則不能進行向下轉型:

    Person per1 = new Person();

    Student stu1 = (Student) per1;

    會出現下面的錯誤:

    Exception in thread "main" java.lang.ClassCastException: human.Person cannot be cast to human.Student
    at human.TestMain.main(TestMain.java:15)

    因為per1指向的是父類對象,stu1是子類引用,而子類的屬性數及方法數是大於父類的,所以是沒法轉換為子類對象的。

    參考下面的內存圖就比較直觀了:

 

3.那么怎么判斷能不能向下轉型呢?

可以先用instanceof判斷父類的對象引用是不是指向了子類的實例,是的話,則可以向下轉型,否則就不可以。

 

作者: 蟬蟬

請尊重作者勞動成果,轉載請在標題注明“轉載”字樣,並標明原文鏈接:

http://www.cnblogs.com/chanchan/p/7863173.html 

 

4.下面是例子,代碼如下:

package human;

public class TestMain {
    public static void main(String[] args) {
        
        Person per = new Person();
        Person per2;
        Student stu = new Student();
        Student stu2;
        
        per2 = stu;
        stu2 = (Student) per2;
//        stu2 = (Student) per;
        
        if( per2 instanceof Student )
            System.out.println("per2指向的是Student類的對象");
        else
            System.out.println("per2指向的不是Student類的對象");
        if( per2 instanceof Person )
            System.out.println("per2指向的是Person類的對象");
        else
            System.out.println("per2指向的不是Person類的對象");
        
        if( per instanceof Student)
            System.out.println("per指向的是Student類的對象");
        else
            System.out.println("per指向的不是Student類的對象");
        if( per instanceof Person )
            System.out.println("per指向的是Person類的對象");
        else
            System.out.println("per指向的不是Person類的對象");
        
        
        if( stu2 instanceof Student )
            System.out.println("stu2指向的是Student類的對象");
        else
            System.out.println("stu2指向的不是Student類的對象");
        if( stu2 instanceof Person )
            System.out.println("stu2指向的是Person類的對象");
        else
            System.out.println("stu2指向的不是Person類的對象");
        
        if( stu instanceof Student )
            System.out.println("stu指向的是Student類的對象");
        else
            System.out.println("stu指向的不是Student類的對象");
        if( stu instanceof Person )
            System.out.println("stu指向的是Person類的對象");
        else
            System.out.println("stu指向的不是Person類的對象");
    }

}

輸出結果如下:

1 per2指向的是Student類的對象
2 per2指向的是Person類的對象
3 per指向的不是Student類的對象
4 per指向的是Person類的對象
5 stu2指向的是Student類的對象
6 stu2指向的是Person類的對象
7 stu指向的是Student類的對象
8 stu指向的是Person類的對象

結果分析:

(1).per2 = stu;

     stu2 = (Student) per2;

per2與stu指向了同一子類對象,所以可以進行向下轉型。

(2).// stu2 = (Student) per;

由於per指向的是Person類的對象,所以向下轉型會出錯。

(3).從1,2,5,6,7,8行輸出可以看出,使用instanceof時,不論a定義為父類的對象引用還是子類的對象引用,只要最終指向的是子類對象,instanceof判定它既是父類的實例也是子類的實例即,可以看成子類實例包含了一個父類實例。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM