黑马程序员技术交流社区

标题: 泛型的问题 [打印本页]

作者: 天方地圆    时间: 2014-4-3 16:18
标题: 泛型的问题
本帖最后由 天方地圆 于 2014-4-5 22:03 编辑
  1. import java.lang.reflect.Method;
  2. import java.lang.reflect.ParameterizedType;
  3. import java.net.Proxy.Type;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.Vector;

  10. public class GenericTest {

  11.         /**
  12.          * @param args
  13.          */
  14.         public static void main(String[] args) throws Exception{
  15.                 // TODO Auto-generated method stub
  16.                 /*
  17.                 ArrayList<String> collection = new ArrayList<String>();
  18.                 collection.add("abc");
  19.                 String element = collection.get(0);
  20.                 System.out.println(element);
  21.                
  22.                
  23.                 ArrayList<Integer> collection2 = new ArrayList<Integer>();
  24.                 System.out.println(collection2.getClass() == collection.getClass() );
  25.                 collection2.getClass().getMethod("add", Object.class).invoke(collection2, "abc");
  26.                 System.out.println(collection2.get(0));
  27.                
  28.                
  29.                
  30.                 HashMap<String,Integer> maps = new HashMap<String,Integer>();
  31.                 maps.put("anadgn", 30);
  32.                 maps.put("dsjgjdsg", 40);
  33.                 maps.put("abdg", 50);
  34.                
  35.                 Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();
  36.                 for(Map.Entry<String,Integer> entry : entrySet){
  37.                         System.out.println(entry.getKey() +":"+ entry.getValue());
  38.                 }
  39.                
  40.                 swap(new String[]{"abc","bdg","itcast"},1,2);
  41.                 //swap(new int[]{1,23,4,5});
  42.                
  43.                 Object obj = "abc";
  44.                 String x3 = autoConvert(obj);
  45.                 */
  46.                 //Vector<Date> v1 = new Vector<Date>();
  47.                 Method applyMethod = GenericTest.class.getMethod("applyMethod",Vector.class);
  48.                 java.lang.reflect.Type[] types = applyMethod.getGenericParameterTypes();
  49.                 ParameterizedType pType = (ParameterizedType)types(0);
  50.                 System.out.println(pType.getRawType());
  51.                 System.out.println(pType.getActualTypeArguments()[0]);
  52.                
  53.                
  54.                
  55.         }
  56.         public static void applyMethod(Vector<Date> v1){
  57.                
  58.         }
  59. }
复制代码


作者: 天方地圆    时间: 2014-4-3 16:20
运行报出,空指针异常,是怎么回事?
作者: 一年_Hei    时间: 2014-4-3 16:38
第51行types(0);改为types[0];




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2