泛型:用<X>泛化成员和方法的类型
一、泛型方法:泛化的类型在方法内使用
-
<E> void fun(E a){}
-
<E>在方法类型前面
二、泛型类:泛化的类型在类内使用
-
class className <E>{}
-
<E>在类名后面
三、类型通配符: 使用?代替具体的类型参数
public class Test<A>{
A a;
void setA(A a) {
this.a=a;
}
void printA() {
System.out.println(a);
}
public <E> void fun(E index){
System.out.println(index);
}
void foo(Test<?> data) {
System.out.println(data.a);
}
public static void main(String args[]) {
Test<String> t=new Test<String>();
t.setA("hello");
t.printA();
t.fun(123);
t.foo(t);
}
}