首页 > 代码库 > 泛型的上下限定义
泛型的上下限定义
class Info<T>{
private T var;
public T getVar(){
return var;
}
public void setVar(T var){
this.var=var;
}
public String toString(){
return this.var.toString();
}
}
public class GenericsDemo17 {
public static void main(String[] args) {
Info<Integer> i1=new Info<Integer>();
Info<Float> i2=new Info<Float>();
Info<String> i3=new Info<String>();
i1.setVar(30);
i2.setVar(30.1f);
i3.setVar("vonkenmo");
fun1(i1);
fun1(i2);
// fun1(i3);
fun2(i3);
}
public static void fun1(Info<? extends Number> i3){
System.out.println(i3+",");
}
public static void fun2(Info<? super String> i3){
System.out.println(i3+",");
}
}