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: 20Data 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 TSample<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>类型设置为类型参数的上限,即此方法接受所有(整数类型的)集合对象。
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);
}
}
输出结果
2456
89
75
36
现在,如果将collection以外的其他类型作为类型化参数传递给此方法,则会生成编译时错误。
示例
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
以上是 Java中泛型的有界类型? 的全部内容, 来源链接: utcz.com/z/322500.html