class Demo1{ public T fun(T t){ return t; } } class Demo2{ public <T> T fun(T t){ return t; } } class Demo3{ public static <T> T fun(T t){ return t; } } class Demo4{ public static T fun(T t){ return t; } } class Demo5<T>{ public T fun(T t){ return t; } } class Demo6<T>{ public <T> T fun(T t){ return t; } } class Demo7<T>{ public static <T> T fun(T t){ return t; } } class Demo8<T>{ public static T fun(T t){ return t; } }
1,4,报错T cannot be resolved to a type。
8报错Cannot make a static reference to the non-static type T
6出现The type parameter T is hiding the type T 使用了@SuppressWarnings("hiding")就好了。 do you kown why?
泛型方法所在的类可以是泛型类,也可以是非泛型类。对于static方法,由于无法访问泛型类中的类型参数,所以static如果想使其具备泛型能力,必须使其成为泛型方法。
原因不是很清楚,模模糊糊,先记下来,以后在研究....
-----------------------------------------------------
转载请注明来源此处
原地址:#
发表