java基础之泛型详解
Set接口的实现类ArrayList详解
(一).泛型介绍:
泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型。参数化类型,把类型当作参数一样的传递。
(i).格式:<数据类型> 此处的数据类型只能是引用类型。
(ii).好处:
A:把运行时期的问题(判断数据类型)提前到了编译期间
B:避免了强制类型转换
C:优化了程序设计
(二).泛型类:把类定义在泛型上
1 ObjectTool.java; 2 3 /* 4 * 泛型类:把泛型定义在类上 5 */ 6 public class ObjectTool<T> { 7 private T obj; 8 9 public T getObj() { 10 return obj; 11 } 12 13 public void setObj(T obj) { 14 this.obj = obj; 15 } 16 }
1 ObjectToolDemo.java 2 3 /* 4 * 泛型类的测试 5 */ 6 public class ObjectToolDemo { 7 public static void main(String[] args) { 8 //类不是泛型情况下,要用同类型来接收 9 /* ObjectTool ot = new ObjectTool(); 10 ot.setObj(new String("张三")); 11 String s = (String) ot.getObj(); 12 System.out.println("姓名是:" + s); 13 14 ot.setObj(new Integer(30)); 15 Integer i = (Integer) ot.getObj(); 16 System.out.println("年龄是:" + i); 17 18 ot.setObj(new String("李四")); 19 // 报ClassCastException异常 20 Integer ii = (Integer) ot.getObj(); 21 System.out.println("姓名是:" + ii);*/ 22 23 //泛型类应用 24 ObjectTool<String> ot = new ObjectTool<String>(); 25 // ot.setObj(new Integer(27)); //这个时候编译期间就过不去 26 ot.setObj(new String("张三")); 27 String s = ot.getObj(); 28 System.out.println("姓名是:" + s); 29 30 ObjectTool<Integer> ot2 = new ObjectTool<Integer>(); 31 // ot2.setObj(new String("张三"));//这个时候编译期间就过不去 32 ot2.setObj(new Integer(27)); 33 Integer i = ot2.getObj(); 34 System.out.println("年龄是:" + i); 35 } 36 }
(三).泛型定义在方法上:
1 ObjectTool.java; 2 3 public class ObjectTool<T> { 4 /* 5 * 非泛型方法的定义,不同类型要写不同的方法 6 public void show(String s) { 7 System.out.println(s); 8 } 9 10 public void show(T t) { 11 System.out.println(t); 12 } 13 } 14 */ 15 /* 16 * 泛型方法:把泛型定义在方法上 17 */ 18 public class ObjectTool { 19 public <T> void show(T t) { 20 System.out.println(t); 21 } 22 }
1 ObjectToolDemo.java; 2 3 public class ObjectToolDemo { 4 public static void main(String[] args) { 5 // ObjectTool ot = new ObjectTool(); 6 // ot.show("hello"); 7 // ot.show(100); 8 // ot.show(true); 9 10 // ObjectTool<String> ot = new ObjectTool<String>(); 11 // ot.show("hello"); 12 // 13 // ObjectTool<Integer> ot2 = new ObjectTool<Integer>(); 14 // ot2.show(100); 15 // 16 // ObjectTool<Boolean> ot3 = new ObjectTool<Boolean>(); 17 // ot3.show(true); 18 19 // 定义泛型方法后 20 ObjectTool ot = new ObjectTool(); 21 ot.show("hello"); 22 ot.show(100); 23 ot.show(true); 24 } 25 }
(四).泛型定义在接口上:
直接上代码,上了你就明白了
Inter.java; /* * 泛型接口:把泛型定义在接口上 */ public interface Inter<T> { public abstract void show(T t); }
InterImpl.java; //普通的方式和泛型接口实现 //实现类在实现接口的时候 //第一种情况:已经知道该是什么类型的了 //public class InterImpl implements Inter<String> { // // @Override // public void show(String t) { // System.out.println(t); // } // } //第二种情况:还不知道是什么类型的,根据自己的需求来变化 public class InterImpl<T> implements Inter<T> { @Override public void show(T t) { System.out.println(t); } }
(五).泛型高级(通配符):
1 import java.util.ArrayList; 2 import java.util.Collection; 3 4 /* 5 * 泛型高级(通配符) 6 * ?:任意类型,如果没有明确,那么就是Object以及任意的Java类了 7 * ? extends E:向下限定,E及其子类 8 * ? super E:向上限定,E极其父类 9 */ 10 public class GenericDemo { 11 public static void main(String[] args) { 12 // 泛型如果明确的写的时候,前后必须一致 13 Collection<Object> c1 = new ArrayList<Object>(); 14 // Collection<Object> c2 = new ArrayList<Animal>();//出错 15 // Collection<Object> c3 = new ArrayList<Dog>();//出错 16 // Collection<Object> c4 = new ArrayList<Cat>();//出错 17 18 // ?表示任意的类型都是可以的 19 Collection<?> c5 = new ArrayList<Object>(); 20 Collection<?> c6 = new ArrayList<Animal>(); 21 Collection<?> c7 = new ArrayList<Dog>(); 22 Collection<?> c8 = new ArrayList<Cat>(); 23 24 // ? extends E:向下限定,E及其子类,上界最高也只能到E 25 // Collection<? extends Animal> c9 = new ArrayList<Object>();//出错 26 Collection<? extends Animal> c10 = new ArrayList<Animal>(); 27 Collection<? extends Animal> c11 = new ArrayList<Dog>(); 28 Collection<? extends Animal> c12 = new ArrayList<Cat>(); 29 30 // ? super E:向上限定,E及其父类,下界最低也要有E 31 Collection<? super Animal> c13 = new ArrayList<Object>(); 32 Collection<? super Animal> c14 = new ArrayList<Animal>(); 33 // Collection<? super Animal> c15 = new ArrayList<Dog>();//出错 34 // Collection<? super Animal> c16 = new ArrayList<Cat>();//出错 35 } 36 } 37 38 class Animal { 39 } 40 41 class Dog extends Animal { 42 } 43 44 class Cat extends Animal { 45 }

浙公网安备 33010602011771号