泛型:
总结:使用?通配符可以引用其他各种参数化的类型,?通配符定义的变量主要用作引用,可以调用与参数化无关的方法,不能调用与参数化有关的方法。通配符的扩展?上边界: .//new 指向子类Vector<? extends Number> x = new Vector<Integer>();下边界 : //new 指向父类Vector<? super Integer> x = new Vector<Number>();提示:限定通配符总是包括自己。
1//只有引用类型才能作为泛型方法的实际参数2.普通方法,构造方法和静态方法中都可以使用泛型。编译器也不允许创建类型变量的数组。3.泛型中可以同时有多个类型参数,在定义他们的尖括号中用逗号分开,例如:public static <K,V> getValue(K key){return map.get(ke)}4.编译器不允许创建类型变量的数组。即在创建数组实例时,数组的元素不能使用参数化的类型。例如Vector<Integer> vectorList[] = new Vector<Integer>[10]; //不行,编译器报错5.下面的代码说明对异常如何采用泛型private static <T extends Exception> sayHello() throws T{try{} catch(Exception e){ throw (T)e;}}
GenericTest.java
package ZHANG.GENERIC; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Vector; public class GenericTest { public static void main(String[] args) throws Exception, NoSuchMethodException { ArrayList list = new ArrayList(); list.add(1); list.add(1L); list.add("abc"); int i = (Integer)list.get(0); System.out.println(i); ArrayList<String> list2 = new ArrayList<String>(); list2.add("1abc"); list2.add("ddd"); list2.add("abc"); String element = list2.get(1); System.out.println(element); //得到String的构造函数 Constructor<String> constructor1 = String.class.getConstructor(StringBuffer.class); String str = constructor1.newInstance(new StringBuffer("abc")); System.out.println(str.charAt(2)); ArrayList<Integer> list3 = new ArrayList<Integer>(); System.out.println(list3.getClass() == list2.getClass()); //list3.add("abc"); 不行, list3.getClass().getMethod("add", Object.class).invoke(list3, "abc"); System.out.println(list3.get(0)); printCollection(list3); //Class<>String.class.asSubclass(Number.class); Class<?> y ; Class<String> x = null ;//Class.forName("java.lang.String"); y = x ; //迭代HashMap HashMap<String,Integer> maps = new HashMap<String,Integer>(); maps.put("wang", 26); maps.put("chandong", 33); maps.put("baiyan", 23); //开始迭代,获得Set集合 Set<Map.Entry<String,Integer>> entrySet = maps.entrySet(); for(Map.Entry<String,Integer> entry: entrySet){ System.out.println(entry.getKey()+"--->"+entry.getValue()); //从结果发现是按key降序排序 } //自定义的泛型,,只作语法说明//取最大公约数 add(3,5); Number num1 = add(3.5,3); Object num2 = add(3,"abc"); //泛型,交换任意类型 swap(new String[]{"abc","xyz","heima"},1,2); //swap(new int[]{1,5,22},2,4); 这里编译错误,不能是基本类型,只能是引用类型 Object obj = "abc"; String x3 = autoConvert(obj);//根据定义类型返回相应类型 copy1(new Vector<String>(),new String[10]); copy2(new Date[10],new String[10]); //copy1(new Vector<Date>(),new String[10]); 这个报错 ,T类型不一直 } //====================以下为静态方法=========================== // 数组拷贝到集合,数组拷贝到数组 private static <T> void copy1(Collection<T> dest,T[] src){ for(int i=0;i<src.length;i++){ dest.add(src[i]); } for(Object obj:dest){ System.out.println(obj); } } private static <T> void copy2(T[] dest,T[] src){ for(int i=0;i<src.length;i++){ dest[i]=src[i]; } for(T obj:dest){ System.out.println(obj); } } //打印出任意参数化类型的集合中的所有元素 public static <T>void printAnyCollection(Collection<T> collection,T obj2){ //collection.add(1);add方法与类型有关系 System.out.println(collection.size()); for(Object obj:collection){ System.out.println(obj); } //collection.add(obj2); 传进来打印. } //定义一个方法,将任意类型数组中所有元素填充为相应的某个对象 private static <T> void fillArray(T[] a,T obj){ for(int i=0;i<a.length;i++){ a[i] = obj; } } //写一个方法自动将Object类型转换成任意类型 private static <T> T autoConvert(Object obj){ return (T)obj; } //交换2元素的位置 private static <T> void swap(T[] a,int i,int j){ T tmp = a[i]; a[i] = a[j]; a[j] = tmp; } //定义一个类型T的这种变量相加返回还是T这种类型 private static <T>T add(T x,T y){ return null; } //打印任意类型的集合 通配符方式? public static void printCollection(Collection<?> collection){ //collection.add(1);add方法与类型有关系 System.out.println(collection.size()); for(Object obj:collection){ System.out.println(obj); } } }
=====================泛型定义在类上 ,多个方法用于同一个类型,
package ZHANG.GENERIC; import java.util.Date; import java.util.Set; //dao data access object-->crud //泛型定义在类上 public class GenericDAO<T> { // public <T> void add(T x){ // // } public void add(T x){ } public T findById(int id){ return null; } public void delete(T obj){ } public void delete(int id){ } public void update(T obj){ } public static <T> void update2(T obj){ } public <T> boolean findByUserName(String name,String pwd){ return true; } public Set<T> findByConditions(String where){ return null; } public static void main(String[] args) { // GenericDAO dao = new GenericDAO(); // dao.add(new Date()); // String s = (String) dao.findById(1); GenericDAO<Student> dao = new GenericDAO<Student>(); dao.add(new Student()); Student s = (Student) dao.findById(1); } }