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

被折叠的 条评论
为什么被折叠?



