2 我對 <T extends Comparable<? super T>> 型別參數的理解
光看上面的定義除了摸不著頭腦,不會有其它感覺。下面用代碼來說明為什麼要這樣聲明。
2.1 代碼運行環境
我使用的 JDK 版本是: 1.8.0_60 ,在 Eclipse 中編譯運行。因為注釋用了中文,編碼採用 UTF-8。如果你要在命令列下編譯、運行,編譯時間要使用 -encoding UTF-8 選項:
javac -encoding UTF-8 TypeParameterTest.java
另外,Eclipse 中的警告、錯誤資訊跟命令列中的不一樣(個人感覺 Eclipse 中的資訊要好懂一些)。以下的樣本以 Eclipse 中的資訊為準。
2.2 範例程式碼
1: package generics3; 2: 3: import java.util.ArrayList; 4: import java.util.Collections; 5: import java.util.List; 6: 7: public class TypeParameterTest 8: { 9: //第一種聲明:簡單,靈活性低10: public static <T extends Comparable<T>> void mySort1(List<T> list)11: {12: Collections.sort(list);13: }14: 15: //第二種聲明:複雜,靈活性高16: public static <T extends Comparable<? super T>> void mySort2(List<T> list)17: {18: Collections.sort(list);19: }20: 21: public static void main(String[] args)22: {23: //在這個方法中要建立一個 Animal List 和一個 Dog List,然後分別調用兩個排序方法。24: }25: }26: 27: class Animal implements Comparable<Animal>28: {29: protected int age;30: 31: public Animal(int age)32: 33: {34: this.age = age;35: }36: 37: //使用年齡與另一執行個體比較大小38: @Override39: public int compareTo(Animal other)40: {41: return this.age - other.age;42: }43: }44: 45: class Dog extends Animal46: {47: public Dog(int age)48: {49: super(age);50: }51: }
上面的程式碼封裝括三個類:
Animal 實現了 Comparable<Animal> 介面,通過年齡來比較執行個體的大小
Dog 繼承自 Animal 。
TypeParameterTest 類中提供了兩個排序方法和測試用的 main() 方法:
mySort1() 使用 <T extends Comparable<T>> 型別參數
mySort2() 使用 <T extends Comparable<? super T>> 型別參數
main() 測試方法。在這個方法中要建立一個 Animal List 和一個 Dog List ,然後分別調用兩個排序方法
2.3 測試 mySort1() 方法
1: // 建立一個 Animal List 2: List<Animal> animals = new ArrayList<Animal>(); 3: animals.add(new Animal(25)); 4: animals.add(new Dog(35)); 5: 6: // 建立一個 Dog List 7: List<Dog> dogs = new ArrayList<Dog>(); 8: dogs.add(new Dog(5)); 9: dogs.add(new Dog(18));10: 11: // 測試 mySort1() 方法12: mySort1(animals);13: mySort1(dogs);
Line 13 出編譯錯誤了。Eclipse 說:
The method mySort1(List<T>) in the type TypeParameterTest is not applicable for the arguments (List<Dog>)
為什麼會出錯誤呢? mySort1() 方法的型別參數是 <T extends Comparable<T>> ,它要求的型別參數是類型為 T 的 Comparable 。
如果傳入的是 List<Animal> ,沒問題,因為 Animal implements Comparable<Animal> 。
但是,如果傳入的參數是 List<Dog> 有問題,因為 Dog 沒有 implements Comparable<Dog> ,它只是從 Animal 繼承了一個 Comparable<Animal> 。
不知道大家注意到沒有,那個 animals list 中實際上是包含一個 Dog 執行個體的。如果你碰上類似的情況(子類 list 不能傳入到一個方法中),可以考慮把子類執行個體放到一個父類 List 中,避免編譯錯誤。
2.4 測試 mySort2() 方法
1: public static void main(String[] args) 2: { 3: // 建立一個 Animal List 4: List<Animal> animals = new ArrayList<Animal>(); 5: animals.add(new Animal(25)); 6: animals.add(new Dog(35)); 7: 8: // 建立一個 Dog List 9: List<Dog> dogs = new ArrayList<Dog>();10: dogs.add(new Dog(5));11: dogs.add(new Dog(18));12: 13: // 測試 mySort2() 方法14: mySort2(animals);15: mySort2(dogs);16: }
兩個方法調用都沒有問題。 第二個方法不但可以接受 Animal implements Comparable<Animal> 這樣的參數,也可以接收: Dog implements Comparable<Animal> 這樣的參數。
2.5 Dog 可以 implements Comparable<Dog> 嗎?
如果讓 Dog implements Comparable<Dog> 不也可以解決前面的那個編譯錯誤嗎?
1: class Dog extends Animal implements Comparable<Dog>2: {3: public Dog(int age)4: {5: super(age);6: }7: }
很不幸,出錯了。Eclipse 說:
The interface Comparable cannot be implemented more than once with different arguments: Comparable<Animal> and Comparable<Dog>
就是說,Dog 已經從父類 Animal 那裡繼承了一個 Comparable ,它不能再實現一個 Comparable 。
如果子類不喜歡父類的實現怎麼辦? Override 父類的 public int compareTo(Animal other) 方法。
2.6 <T extends Comparable<? super T>> 型別參數聲明的好處
對 Animal/Dog 這兩個有父子關係的類來說: <T extends Comparable<? super T>> 可以接受 List<Animal> ,也可以接收 List<Dog> 。 而 <T extends Comparable<T>> 只可以接收 List<Animal>
所以,<T extends Comparable<? super T>> 這樣的型別參數對所傳入的參數限制更少,提高了 API 的靈活性。總的來說,在保證型別安全的前提下,要使用限制最少的型別參數。