Java 泛型中的有界类型?
java 8object oriented programmingprogramming更新于 2024/10/13 11:11:00
每当您想要将类型参数限制为特定类的子类型时,都可以使用有界类型参数。如果您仅将类型(类)指定为有界参数,则当前泛型类仅接受该特定类的子类型。这些在 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),则会产生编译时错误erated.
示例
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