Java 面向對象-繼承中的注意點

類繼承中父類的構造和子類的構造使用

在Java中,任何class的構造方法,第一行語句必須是調用父類的構造方法。

如果沒有明確地調用父類的構造方法,編譯器會幫我們自動加一句super();,

public class Main {
    public static void main(String[] args) {
        Student s = new Student("Xiao Ming", 12, 89);
    }
}

class Person {
    protected String name;
    protected int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

class Student extends Person {
    protected int score;

    public Student(String name, int age, int score) {
        super(name, age); // 調用父類的構造方法Person(String, int)
        this.score = score;
    }
}

如果父類沒有默認的構造方法,子類就必須顯式調用super()並給出參數以便讓編譯器定位到父類的一個合適的構造方法

向上轉型

如果一個引用變量的類型是Student,那麼它可以指向一個Student類型的實例:

Student s = new Student();

如果一個引用類型的變量是Person,那麼它可以指向一個Person類型的實例:

Person p = new Person();

現在問題來了:如果Student是從Person繼承下來的,那麼,一個引用類型爲Person的變量,能否指向Student類型的實例?

Person p = new Student(); // ???

測試一下就可以發現,這種指向是允許的!

因爲Student繼承自Person,因此,它擁有Person的全部功能。

Person類型的變量,如果指向Student類型的實例,對它進行操作,是沒有問題的!

這種把一個子類類型安全地變爲父類類型的賦值,被稱爲向上轉型(upcasting)。

向上轉型實際上是把一個子類型安全地變爲更加抽象的父類型:

Student s = new Student();
Person p = s; // upcasting, ok
Object o1 = p; // upcasting, ok
Object o2 = s; // upcasting, ok

注意到繼承樹是Student > Person > Object,所以,可以把Student類型轉型爲Person,或者更高層次的Object

向下轉型

和向上轉型相反,如果把一個父類類型強制轉型爲子類類型,就是向下轉型(downcasting)。例如:

Person p1 = new Student(); // upcasting, ok
Person p2 = new Person();
Student s1 = (Student) p1; // ok
Student s2 = (Student) p2; // runtime error! ClassCastException!

如果測試上面的代碼,可以發現:

Person類型p1實際指向Student實例,Person類型變量p2實際指向Person實例。在向下轉型的時候,把p1轉型爲Student會成功,因爲p1確實指向Student實例,把p2轉型爲Student會失敗,因爲p2的實際類型是Person,不能把父類變爲子類,因爲子類功能比父類多,多的功能無法憑空變出來。

因此,向下轉型很可能會失敗。失敗的時候,Java虛擬機會報ClassCastException

爲了避免向下轉型出錯,Java提供了instanceof操作符,可以先判斷一個實例究竟是不是某種類型:

Person p = new Person();
System.out.println(p instanceof Person); // true
System.out.println(p instanceof Student); // false

Student s = new Student();
System.out.println(s instanceof Person); // true
System.out.println(s instanceof Student); // true

Student n = null;
System.out.println(n instanceof Student); // false

instanceof實際上判斷一個變量所指向的實例是否是指定類型,或者這個類型的子類。

如果一個引用變量爲null,那麼對任何instanceof的判斷都爲false

區分繼承和組合 

在使用繼承時,我們要注意邏輯一致性。

考察下面的Book類:

class Book {
    protected String name;
    public String getName() {...}
    public void setName(String name) {...}
}

這個Book類也有name字段,那麼,我們能不能讓Student繼承自Book呢?

class Student extends Book {
    protected int score;
}

顯然,從邏輯上講,這是不合理的,Student不應該從Book繼承,而應該從Person繼承。

究其原因,是因爲StudentPerson的一種,它們是is關係,而Student並不是Book。實際上StudentBook的關係是has關係。

具有has關係不應該使用繼承,而是使用組合,即Student可以持有一個Book實例:

class Student extends Person {
    protected Book book;
    protected int score;
}

因此,繼承是is關係,組合是has關係。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章