1.在方法中定义
public class ReGen1 <T>{
T ob; //定义泛型的成员变量ob
public ReGen1(T ob){ //参数化
this.ob = ob;
}
public void showType(){
System.out.println("T的实际类型是:"+ob.getClass().getName());
System.out.println("值是:"+ob);
}
public static void main(String[] args) {
new ReGen1<Integer>(4854).showType();
new ReGen1<Boolean>(true).showType();
new ReGen1<String>("afe").showType();
}
}
2..在类中定义
public class ReGenericTest {
public static <A,B> void myMui(A a,B b){
if(a.equals(b))
System.out.println(a+"与"+b+"相等");
else
System.out.println(a+"与"+b+"不等");
}
public static void main(String[] args) {
myMui("2",2);
myMui(2,2);
myMui("2","2");
}
}