Java中的泛型
概念:给类一个类型。实现类的通用。
实现:
public class General <T> {
private T x;
public General (T x){
this.x =x;
}
public T getX()
{
return x;
}
public void setX(T x){
this.x =x;
}
public void showType()
{
System.out.println("实际使用的类型:"+x.getClass().getName());
}
}
运行:
public class 泛型 {
public static void main(String[] args) {
General<Integer> gen = new General<Integer>(new Integer(10));
int x= gen.getX();
System.out.println(x);
gen.showType();
}
}