在 Java 中,我们如何将泛型(类型参数)限制为特定类的子类?

java 8object oriented programmingprogramming更新于 2025/4/15 9:52:17

每当您想要将类型参数限制为特定类的子类型时,都可以使用有界类型参数。如果您仅将类型(类)指定为有界参数,则当前泛型类只接受该特定类的子类型。

您只需使用类型参数扩展所需类即可声明绑定参数,在尖括号内为 −

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

多个界限

您还可以使用 extends 关键字将多个类型列为有界参数,并使用 “&” 将它们分开。传递给此类的 (type) 参数应为所有指定类的子类型。

示例

在下面的示例中,我们设置了 Number 和 Comparable 类型的上限,即此类接受两个类的子类类型。

class Sample <T extends Number & Comparable<T> >{
   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>(22);
      obj1.display();
      Sample<Double> obj2 = new Sample<Double>(20.22d);
      obj2.display();
   }
}

输出

Data value is: 22
Data value is: 20.22

如果将 AtomicInteger 类作为参数传递给 Sample 类,由于它不是可比较类的子类型,因此会产生编译时错误

示例

import java.util.concurrent.atomic.AtomicInteger;
class Sample <T extends Number & Comparable<T> >{
   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>(22);
      obj1.display();
      Sample<Double> obj2 = new Sample<Double>(20.22d);
      obj2.display();
      Sample<AtomicInteger> obj3 = new Sample<AtomicInteger>(124);
      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

相关文章