赞
踩
public class Main<T> { private T obj; public T getObj() { return obj; } public void setObj(T obj){ this.obj=obj; } public static void main(String[] args){ Main<String> name=new Main<String>(); Main<Integer> age=new Main<Integer>(); name.setObj("张三"); String newName=name.getObj(); System.out.println("姓名"+newName); age.setObj(25); int newAge=age.getObj(); System.out.println("年龄:"+newAge); } }
泛型方法的应用:
public class Main { public static void main(String[] args){ Integer[] num={1,2,3,4,5}; String[] str={"红","蓝","紫"}; Main.display(num); Main.display(str); } public static <E> void display(E[] list){//E为类型参数 for(int i=0;i<list.length;i++) System.out.println(list[i]+" "); System.out.println(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。