泛型
泛型类: 是在实例化的时候指明泛型的具体类型。
pubilc class GenericClass<T> {
private T attr;
pubilc void setValue(T a) {
this.attr = a;
}
public T getValue() {
return this.attr;
}
}
泛型方法: 是在调用方法的时候指明泛型的具体类型.
public class GenericMethod<T> {
private T key;
public GenericMethod(T key) {
this.key = key;
}
public T getKey() {
return key;
}
public <N extends Number> N showKeyValue1(Generic<N> obj) {
return obj.getKey();
}
public void showKeyValue1(Generic<Number> obj) {
System.out.println("container key" + obj.getKey());
}
public void showKeyValue2(Generic<?> obj) {
System.out.println("container key" + obj.getKey());
}
}