/** * @author Rollen-Holt 使用泛型 */ class hello<T, V> { hello(){ } public T getName(){ return name; } public void setName(T name){ this.name = name; } public V getAge(){ return age; } public void setAge(V age){ this.age = age; } private T name; private V age; public static void main(String[] args){ hello<String, Integer> he = new hello<String, Integer>(); he.setAge(10); he.setName("Rollen Holt"); System.out.println(he.getName() + " " + he.getAge()); } } /** * @author Rollen-Holt 泛型类的构造方法定义 */ class hello<T, V> { hello(T name, V age){ this.age = age; this.name = name; } public T getName(){ return name; } public V getAge(){ return age; } private T name; private V age; public static void main(String[] args){ hello<String,Integer> he=new hello<String,Integer>("Rollen",12); System.out.println(he.getName()+" "+he.getAge()); } } /** * @author Rollen-Holt 使用通配符 */ class info<T> { info(T name){ this.name = name; } private T name; } class hello{ public static void function(info<?> temp){ System.out.println("内容: "+temp); } public static void main(String[] args){ info<String> demo=new info<String>("Rollen"); function(demo); } } /** * @author Rollen-Holt 泛型上限 */ class info<T>{ info(T age){ this.age=age; } private T age; } class hello{ public static void function(info<? extends Number> temp){ System.out.println("内容"+ temp); } public static void main(String[] args){ info<Integer> demo=new info<Integer>(1); function(demo); } } |
HM张博文 发表于 2013-5-26 11:14
泛型是个神马东东,这个我也要学习下哦
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |