构造函数也可以声明类型参数
public class Main { public static class Foo<T> { public <E> Foo(T object, E object2) { } } public static void main(String[] args) throws Exception { Foo<Integer> foo = new <String> Foo<Integer>(1, "hello"); } }这就是
<String>前面的构造函数调用的目的。它是构造函数的类型参数。
下列
Foo<Integer> foo = new <String> Foo<Integer>(1, new Object());
失败于
Main.Foo类型的参数化构造函数Foo(Integer,String)不适用于参数(Integer,Object)
在你的最后
Foo<Integer> t5 = new <NotDefined> Foo<Integer>(); // fails -- NotDefined is undefined
NotDefined只是在编译过程中找不到的类型。如果是的话,只会警告您它是
unused。



