A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 天方地圆 中级黑马   /  2014-4-3 16:18  /  1036 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 天方地圆 于 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. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

2 个回复

正序浏览
第51行types(0);改为types[0];
回复 使用道具 举报
运行报出,空指针异常,是怎么回事?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马