Java泛型中的限定型別?


當您希望將型別引數限制為特定類的子型別時,可以使用限定型別引數。如果您僅將某個型別(類)指定為限定引數,則當前泛型類僅接受該特定類的子型別。這些在 Java 泛型中被稱為限定型別。

為類定義限定型別

您可以透過在尖括號內使用型別引數擴充套件所需的類來宣告限定引數,例如:

class Sample <T extends Number>

示例

在下面的 Java 示例中,泛型類 Sample 使用限定引數將型別引數限制為 Number 類的子類。

 線上演示

class Sample <T extends Number>{
   T data;
   Sample(T data){
      this.data = data;
   }
   public void display() {
      System.out.println("Data value is: "+this.data);
   }
}
public class BoundsExample {
   public static void main(String args[]) {
      Sample<Integer> obj1 = new Sample<Integer>(20);
      obj1.display();
      Sample<Double> obj2 = new Sample<Double>(20.22d);
      obj2.display();
      Sample<Float> obj3 = new Sample<Float>(125.332f);
      obj3.display();
   }
}

輸出

Data value is: 20
Data value is: 20.22
Data value is: 125.332

現在,如果您將其他型別作為引數傳遞給此類(例如 String),則會生成編譯時錯誤。

示例

 線上演示

public class BoundsExample {
   public static void main(String args[]) {
      Sample<Integer> obj1 = new Sample<Integer>(20);
      obj1.display();
      Sample<Double> obj2 = new Sample<Double>(20.22d);
      obj2.display();
      Sample<String> obj3 = new Sample<String>("Krishna");
      obj3.display();
   }
}

編譯時錯誤

BoundsExample.java:16: error: type argument String is not within bounds of type-variable T
      Sample<String> obj3 = new Sample<String>("Krishna");
            ^
   where T is a type-variable:
      T extends Number declared in class Sample
BoundsExample.java:16: error: type argument String is not within bounds of type-variable T
      Sample<String> obj3 = new Sample<String>("Krishna");                                 ^
   where T is a type-variable:
      T extends Number declared in class Sample
2 errors

為方法定義限定型別

與類一樣,要為泛型方法定義限定型別引數,請在 extend 關鍵字之後指定它們。如果您傳遞的型別不是指定限定型別的子類,則會生成錯誤。

示例

在下面的示例中,我們將 Collection<Integer> 型別設定為型別引數的上限,即此方法接受所有集合物件(Integer 型別)。

 線上演示

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class GenericMethod {
   public static <T extends Collection<Integer>> void sampleMethod(T ele){
      Iterator<Integer> it = ele.iterator();
      while (it.hasNext()) {
         System.out.println(it.next());
      }
   }
   public static void main(String args[]) {
      ArrayList<Integer> list = new ArrayList<Integer>();
      list.add(24);
      list.add(56);
      list.add(89);
      list.add(75);
      list.add(36);
      sampleMethod(list);
   }
}

輸出

24
56
89
75
36

現在,如果您將除集合之外的其他型別作為型別引數傳遞給此方法,則會生成編譯時錯誤。

示例

 線上演示

public class GenericMethod {
   public static <T extends Collection<Integer>> void sampleMethod(T ele){
      Iterator<Integer> it = ele.iterator();
         while (it.hasNext()) {
            System.out.println(it.next());
         }
      }
   public static void main(String args[]) {
      ArrayList<Integer> list = new ArrayList<Integer>();
      list.add(24);
      list.add(56);
      list.add(89);
      list.add(75);
      list.add(36);
      sampleMethod(list);
      Integer [] intArray = {24, 56, 89, 75, 36};
      sampleMethod(intArray);
   }
}

編譯時錯誤

GenericMethod.java:23: error: method sampleMethod in class GenericMethod cannot be applied to given types;
      sampleMethod(intArray);
      ^
   required: T
   found: Integer[]
   reason: inferred type does not conform to upper bound(s)
      inferred: Integer[]
      upper bound(s): Collection<Integer>
   where T is a type-variable:
      T extends Collection<Integer> declared in method <T>sampleMethod(T)
1 error

更新於: 2019年9月9日

7K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.