我们如何将泛型(类型参数)限制为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关键字将多个类型作为有界参数列出,并使用“&”将它们分隔开。传递给此类的(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