在第6章,大家已知道可將一個對象作為它自己的類型使用,或者作為它的基礎類型的一個對象使用。取得一個對象句柄,並將其作為基礎類型句柄使用的行為就叫作“上溯造型”——因為繼承樹的畫法是基礎類位於最上方。
但這樣做也會遇到一個問題,如下例所示(若執行這個程序遇到麻煩,請參考第3章的3.1.2小節“賦值”):
//: Music.java
// Inheritance & upcasting
package c07;
class Note {
private int value;
private Note(int val) { value = val; }
public static final Note
middleC = new Note(0),
cSharp = new Note(1),
cFlat = new Note(2);
} // Etc.
class Instrument {
public void play(Note n) {
System.out.println("Instrument.play()");
}
}
// Wind objects are instruments
// because they have the same interface:
class Wind extends Instrument {
// Redefine interface method:
public void play(Note n) {
System.out.println("Wind.play()");
}
}
public class Music {
public static void tune(Instrument i) {
// ...
i.play(Note.middleC);
}
public static void main(String[] args) {
Wind flute = new Wind();
tune(flute); // Upcasting
}
} ///:~
其中,方法Music.tune()接收一個Instrument句柄,同時也接收從Instrument衍生出來的所有東西。當一個Wind句柄傳遞給tune()的時候,就會出現這種情況。此時沒有造型的必要。這樣做是可以接受的;Instrument裡的接口必須存在於Wind中,因為Wind是從Instrument裡繼承得到的。從Wind向Instrument的上溯造型可能“縮小”那個接口,但不可能把它變得比Instrument的完整接口還要小。