泛型方法
可以在方法声明中定义类型参数,它们在方法的返回类型之前的尖括号中指定。包含泛型方法声明的类型不必是通用类型。可以在非静态方法声明中使用为泛型类型指定的类型参数。
示例
以下代码显示如何为方法m1()
定义新的类型参数V
。新类型参数V
强制将方法m1()
的第一个和第二个参数必须为相同类型。
第三个参数必须与类型T
相同,这是类实例化的类型。
class MyBag<T> {
private T ref;
public MyBag(T ref) {
this.ref = ref;
}
public T get() {
return ref;
}
public void set(T a) {
this.ref = a;
}
}
class Test<T> {
public <V> void m1(MyBag<V> a, MyBag<V> b, T c) {
}
}
使用通用方法
要传递方法的形式类型参数的实际类型参数,必须在方法调用中的点和方法名之间的尖括号<>
中指定它。
class MyBag<T> {
private T ref;
public MyBag(T ref) {
this.ref = ref;
}
public T get() {
return ref;
}
public void set(T a) {
this.ref = a;
}
}
class Test<T> {
public <V> void m1(MyBag<V> a, MyBag<V> b, T c) {
}
}
public class Main {
public static void main(String[] argv) {
Test<String> t = new Test<String>();
MyBag<Integer> iw1 = new MyBag<Integer>(new Integer(201));
MyBag<Integer> iw2 = new MyBag<Integer>(new Integer(202));
// Specify that Integer is the actual type for the type parameter for m1()
t.<Integer>m1(iw1, iw2, "hello");
t.m1(iw1, iw2, "hello");
}
}
实例-2
以下代码显示了如何声明通用静态方法。不能在静态方法中引用包含类的类型参数。
静态方法只能引用它自己声明的类型参数。以下静态通用类型定义了类型参数T
,用于约束参数source
和dest
的类型。
class MyBag<T> {
private T ref;
public MyBag(T ref) {
this.ref = ref;
}
public T get() {
return ref;
}
public void set(T a) {
this.ref = a;
}
}
public class Main {
public static <T> void copy(MyBag<T> source, MyBag<? super T> dest) {
T value = source.get();
dest.set(value);
}
public static void main(String[] argv) {
}
}
要为静态方法调用指定实际的类型参数,可以这样做:
Main.<Integer>copy(iw1, iw2);
通用构造函数
可以为构造函数定义类型参数。下面的代码定义了类Test
的构造函数的类型参数U
。它放置一个约束,构造函数的类型参数U
必须是相同的,或者它的类类型参数T
类型的子类型。
public class Test<T> {
public <U extends T> Test(U k) {
}
}
要为构造函数指定实际的类型参数值,请在 new
运算符和构造函数名称之间的尖括号中指定它,如以下代码段所示:
class Test<T> {
public <U extends T> Test(U k) {
}
}
public class Main {
public static void main(String[] argv) {
// Specify the actual type parameter for the constructor as Double
Test<Number> t1 = new<Double> Test<Number>(new Double(1.9));
// Let the compiler figure out, Integer is
// the actual type parameter for the constructor
Test<Number> t2 = new Test<Number>(new Integer(1));
}
}
通用对象在创建中的类型推断
Java7在通用类型的对象创建表达式中增加了对类型推断的一些有限支持。
对于以下语句:
List<String> list = new ArrayList<String>();
在Java7
中,可以在上面的语句中指定空尖括号,称为菱形操作符或简单的菱形<>
作为ArrayList
的类型参数。
List<String> list = new ArrayList<>();
如果不在对象创建表达式中为通用类型指定类型参数,那么类型是原始类型,编译器生成未检查的警告。
例如,以下语句将编译产生未选中的警告:
List<String> list = new ArrayList(); // Generates an unchecked warning
不能创建通用的异常类。 并且没有通用的匿名类。
上一篇:
Java泛型约束
下一篇:
Java异常处理教程