欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

黑马程序员-笔记-泛型,黑马程序员笔记

来源: javaer 分享于  点击 46546 次 点评:149

黑马程序员-笔记-泛型,黑马程序员笔记


----------- android培训java培训、java学习型技术博客、期待与您交流! ------------


[java] view plaincopy
  1. public class GenericTest {  
  2.   
  3.     /** 
  4.      * 泛型:JDK1.6新特性。 
  5.      *  
  6.      * @throws NoSuchMethodException 
  7.      * @throws SecurityException 
  8.      * @throws InvocationTargetException 
  9.      * @throws IllegalAccessException 
  10.      * @throws InstantiationException 
  11.      * @throws IllegalArgumentException 
  12.      * @throws ClassNotFoundException 
  13.      * @throws NoSuchFieldException  
  14.      */  
  15.     public static void main(String[] args) throws SecurityException,  
  16.             NoSuchMethodException, IllegalArgumentException,  
  17.             InstantiationException, IllegalAccessException,  
  18.             InvocationTargetException, ClassNotFoundException, NoSuchFieldException {  
  19.         ArrayList list = new ArrayList();  
  20.         list.add(1);  
  21.         list.add('a');  
  22.         list.add("abc");  
  23.         // int i = (Integer) list.get(2);//类型转换错误。  
  24.   
  25.         ArrayList<String> list1 = new ArrayList<String>();  
  26.         list1.add("abc");  
  27.         Constructor<String> con = String.class  
  28.                 .getConstructor(StringBuffer.class);  
  29.         String str = con.newInstance(new StringBuffer("abc"));  
  30.   
  31.         Class<String> c1 = String.class;  
  32.         Class<String> c2 = (Class<String>) Class.forName("java.lang.String");  
  33.   
  34.         genericEqual();  
  35.   
  36.         generic1();  
  37.           
  38.         //genericTest(new int []{1});//泛型数组不能用基本类型代替,基本类型会单独成为一个Object  
  39.         genericTest(new Integer []{1});  
  40.           
  41.     }  
  42.   
  43.     public static void genericEqual() throws SecurityException,  
  44.             NoSuchMethodException, IllegalArgumentException,  
  45.             IllegalAccessException, InvocationTargetException {  
  46.         // 内存中泛型被去类型化,泛型是不会存在于字节码中的。但是它存在于一个Type类型中,Type是Class的父类。  
  47.         ArrayList<String> list1 = new ArrayList<String>();  
  48.         ArrayList<Integer> list2 = new ArrayList<Integer>();  
  49.         System.out.println(list1.getClass() == list2.getClass());  
  50.   
  51.         // 通过反射,从字节码中跳过泛型限制。  
  52.         Method addMehtod = list2.getClass().getMethod("add", Object.class);  
  53.         Method getMehtod = list2.getClass().getDeclaredMethod("get"int.class);  
  54.   
  55.         addMehtod.invoke(list2, "abc");  
  56.         System.out.println(list2.get(0));  
  57.         System.out.println(getMehtod.invoke(list2, 0));  
  58.     }  
  59.   
  60.     public static void generic1() throws SecurityException, NoSuchMethodException, NoSuchFieldException {  
  61.         // 参数化类型与原始类型相互兼容,但注意其影响。  
  62.         List<String> list1 = new ArrayList();  
  63.         list1.add("123");  
  64.         List list2 = new ArrayList<String>();  
  65.         list2.add(123);  
  66.   
  67.         // 参数化类型是不会考虑继承的。以下两种情况都错误。  
  68.         // List<String> list3 = new ArrayList<Object>();  
  69.         // List<Object> list4 = new ArrayList<String>();  
  70.   
  71.         // 在创建数组实例时,数组的元素不能使用参数化类型。  
  72.         // List<String>[] lists = new List<String>[5];  
  73.         List[] lists = new List[5];  
  74.   
  75.         List list5 = new ArrayList<Object>();  
  76.         list5.add(123);  
  77.         List<String> list6 = list5;  
  78.         // 虽然以上的赋值过程过了编译,但是再取出的时候,就会爆出异常。  
  79.         // !!!!!泛型的限制完全取决于左边。  
  80.         // System.out.println(list6.get(0));  
  81.           
  82.         //copy(new ArrayList<String>(),new Integer[5]);//泛型的传递。  
  83.         String str = copy2("123","123");  
  84.           
  85.         applyGeneric(new Vector<Date>());  
  86.     }  
  87.   
  88.     public static void generic2() {  
  89.         // 占位符?的赋值是可以的。  
  90.         Collection<?> cols = new HashSet<String>();  
  91.         // cols.add("123");//错误,通配符?只能调用所有与参数无关的方法。  
  92.         System.out.println(cols.size());  
  93.     }  
  94.   
  95.     // 泛型与异常。  
  96.     public static <T extends Exception> void genericException() throws T {  
  97.         try {  
  98.   
  99.         } catch (Exception e) {// 此处catch出T这种类型的异常,必须要是明确的。  
  100.             throw (T) e;// 可以跑出一个不明确的异常。  
  101.         }  
  102.     }  
  103.   
  104.     // 可以定义多个泛型。  
  105.     public static <X, Y, Z> void genericTest(X[] x) {  
  106.         //泛型数组不能使用基本类型。  
  107.     }  
  108.       
  109.     //反省的类型传播  
  110.     //在这里的B与下边的B可以不是同一个。这些都是不会矛盾的。  
  111.     public static <B> void copy(List<B> listS,B[] a){  
  112.           
  113.     }  
  114.     public static <B> B copy2(B b1,B b2){  
  115.         return b1;  
  116.     }  
  117.       
  118.     //通过反射获得泛型的类型  
  119.     public  static void applyGeneric(Vector<Date> v1) throws SecurityException, NoSuchMethodException, NoSuchFieldException{  
  120.         Method m = GenericTest.class.getMethod("applyGeneric", Vector.class);//通过成员方法获得。  
  121.         Type[] type = m.getGenericParameterTypes();  
  122.         ParameterizedType pType = (ParameterizedType) type[0];  
  123.         System.out.println(pType.getRawType());  
  124.         System.out.println(pType.getActualTypeArguments()[0]);  
  125.           
  126.         Field v = GenericTest.class.getField("v");//通过成员变量获得。  
  127.         Type typeF =v.getGenericType();  
  128.         ParameterizedType pTypeF =(ParameterizedType)typeF;  
  129.         System.out.println(pTypeF.getRawType());  
  130.         System.out.println(pTypeF.getActualTypeArguments()[0]);  
  131.           
  132.     }  
  133.   
  134.     public Vector<String> v = new Vector<String>();  
  135.   
  136. }  
反省的一些基础知识:
[java] view plaincopy
  1. public class Play22 {  
  2.     public static void main(String[] args) {  
  3.         // 泛型的对象在明确泛型的具体类型后,就被固定了。  
  4.         Util_p22<String> up = new Util_p22<String>();  
  5.         up.setE("java");  
  6.         String s = up.getE();  
  7.         // up.setE(123);  
  8.   
  9.         // 如果需要对象的不同方法,可以去操作不同的类型,那么需要在方法上声明泛型。  
  10.         up.show(213);  
  11.   
  12.         up.print(true);  
  13.           
  14.         List<Play22_1<String>> li = new ArrayList<Play22_1<String>>();  
  15.         Play22_1<String> litest = new Play22_1<String>();  
  16.           
  17.         li.add(litest);  
  18.         up.test1(li).test();  
  19.           
  20.         TreeSet<Play22_1> tset = new TreeSet<Play22_1>(new Comparator<Inter_p22>() {  
  21.   
  22.   
  23.             @Override  
  24.             public int compare(Inter_p22 o1, Inter_p22 o2) {  
  25.                 // TODO Auto-generated method stub  
  26.                 return 0;  
  27.             }  
  28.         });  
  29.   
  30.     }  
  31.   
  32. }  
  33.   
  34. // 泛型定义在类上。  
  35. class Util_p22<E> {  
  36.     private E e;  
  37.   
  38.     public E getE() {  
  39.         return e;  
  40.     }  
  41.   
  42.     public void setE(E e) {  
  43.         this.e = e;  
  44.     }  
  45.   
  46.     // 静态方法不能访问类上定义的泛型,因为这个泛型,需要在类的实例被确定后才确定。这个方法必然不能是static。  
  47.     public <T> void show(T t) {  
  48.         System.out.println(t);  
  49.     }  
  50.   
  51.     // 泛型定义在方法上,方法上单独存在的泛型是可以为static。而且可以与类上的泛型共同存在。  
  52.     // 注意书写格式。  
  53.     public static <Q> void print(Q q) {  
  54.         System.out.println(q);  
  55.     }  
  56.   
  57.     // 泛型在方法上的另一种写法。?占位符,不清楚所传入的参数的类型的泛型  
  58.     // 使用?通配符,确定是,无法像正常泛型那样使用一个确定的泛型的对象。不能接受并操作。  
  59.     // 这个参数,可以是List<String>  
  60.     // List<Integer>。但是取出来的值依然是Object,不是正常泛型的String或者Integer。  
  61.     public static void test(List<?> list) {  
  62.         Iterator<?> it = list.iterator();  
  63.         while (it.hasNext()) {  
  64.             System.out.println(it.next());  
  65.         }  
  66.     }  
  67.   
  68.     // 向上限定,可以接受,Inter_p22的子类型。  
  69.     public static <K extends Inter_p22> K test1(List<K> list) {  
  70.         Iterator<K> it = list.iterator();  
  71.         return it.next();  
  72.           
  73.           
  74.     }  
  75.     // 通配符写法  
  76.     public static void test2(List<? extends Inter_p22> list) {  
  77.   
  78.     }  
  79.       
  80.     //向下限定,必须是XX的父类。  
  81.     public static <K extends Play22_2> void test3(List<K> list) {  
  82.   
  83.     }  
  84.     // 通配符写法  
  85.     public static void test4(List<? super Play22_2> list) {  
  86.   
  87.     }  
  88. }  
  89.   
  90. // 泛型定义在接口上。  
  91. interface Inter_p22<T> {  
  92.     public void show(T t);  
  93. }  
  94.   
  95. // 继承泛型接口的类。实现类,依然使用泛型。  
  96. class Play22_1<T> implements Inter_p22<T> {  
  97.     @Override  
  98.     public void show(T t) {  
  99.         System.out.println("Play22_1");  
  100.     }  
  101.       
  102.     public void test(){  
  103.         System.out.println("Play22_test");  
  104.     }  
  105. }  
  106.   
  107. // 继承泛型接口的类。实现类,已经明确泛型。  
  108. class Play22_2 implements Inter_p22<String> {  
  109.     @Override  
  110.     public void show(String t) {  
  111.         System.out.println("Play22_2");  
  112.     }  
  113. }  


相关文章

    暂无相关文章
相关栏目:

用户点评