如何在 Java 中将泛型(类型参数)限制为特定类的子类?
无论何时想要将类型参数限制为特定类的子类型,都可以使用有界类型参数。如果只将类型(类)指定为有界参数,则当前泛型类只接受该特定类的子类型。
可以通过在尖括号内使用 extends 关键字扩展所需的类来声明有界参数,例如:
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 关键字列出多个类型作为有界参数,并使用“&”分隔它们,传递给此类的(类型)参数应为所有指定类的子类型。
示例
在下面的示例中,我们设置了 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 类,由于它不是 Comparable 类的子类型,因此会生成编译时错误。
示例
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
广告