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.