黑马程序员_高新技术

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

<span style="font-size:14px;">package cn.itcast.day2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
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;

import cn.itcast.day1.ReflectPoint;

public class GenericTest {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  ArrayList collection1 = new ArrayList();
  collection1.add(1);
  collection1.add(1L);
  collection1.add("abc");
  //int i = (Integer)collection1.get(1);
  
  ArrayList<String> collection2 = new ArrayList<String>();
  //collection2.add(1);
  //collection2.add(1L);
  collection2.add("abc");
  String element = collection2.get(0); 
  
  //new String(new StringBuffer("abc"));
  Constructor<String> constructor1 = String.class.getConstructor(StringBuffer.class);
  String str2 = constructor1.newInstance(/*"abc"*/new StringBuffer("abc"));
  System.out.println(str2.charAt(2));  
  
  ArrayList<Integer> collection3 = new ArrayList<Integer>();
  System.out.println(collection3.getClass() == collection2.getClass());
  //collection3.add("abc");
  collection3.getClass().getMethod("add", Object.class).invoke(collection3, "abc");
  System.out.println(collection3.get(0));
  
  printCollection(collection3);
  
  //Class<Number> x = String.class.asSubclass(Number.class);
  Class<?> y;
  Class<String> x ;//Class.forName("java.lang.String");
  
  HashMap<String,Integer> maps = new HashMap<String, Integer>();
  maps.put("zxx", 28);
  maps.put("lhm", 35);
  maps.put("flx", 33);
  
  Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();
  for(Map.Entry<String, Integer> entry : entrySet){
   System.out.println(entry.getKey() + ":" + entry.getValue());
  }
  
  add(3,5);
  Number x1 = add(3.5,3);
  Object x2 = add(3,"abc");
  
  swap(new String[]{"abc","xyz","itcast"},1,2);
  //swap(new int[]{1,3,5,4,5},3,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]);
  
  GenericDao<ReflectPoint> dao = new GenericDao<ReflectPoint>();
  dao.add(new ReflectPoint(3,3));  
  //String s = dao.findById(1);
  
</span>



  //Vector<Date> v1 = new Vector<Date>();
  //用反射中的getClass方法来得知 Vector这个包中到底装了什么类型。
            Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class);
//想要得到视频总在下面提到的方法,先要搞到字节码!有了字节码就可以调用getMethod,名字和参数分别是:applyVector  以及参数列表。
  Type[] types = applyMethod.getGenericParameterTypes();
  ParameterizedType pType = (ParameterizedType)types[0];
  System.out.println(pType.getRawType());
  System.out.println(pType.getActualTypeArguments()[0]);
 }
 
 public static void applyVector(Vector<Date> v1){
//通过这个变量自己是没有办法知道它定义的类型的,但是当我们把变量交给一个方法的时候,作为参数去使用
//或者是当做返回值来使用的时候。这个方法提供了一些方法,来获得它的参数列表,并且是以泛型的参数列表来返回。
  //通过方法来得到applyVector来获得后面参数的类型。(这个是注释2)
  //getGenericParameterTypes

<span style="font-size:14px;"> }

 
 private static <T> void fillArray(T[] a,T obj){
  for(int i=0;i<a.length;i++){
   a[i] = obj;
  }
 }
 private static <T> T autoConvert(Object obj){
  return (T)obj;
 }
 private static <T> void swap(T[] a,int i,int j){
  T tmp = a[i];
  a[i] = a[j];
  a[j] = tmp;
 }
 
 private static <T> T add(T x,T y){
  return null;
 }
 
 public static void printCollection(Collection<?> collection){
  //collection.add(1);
  System.out.println(collection.size());
  for(Object obj : collection){
   System.out.println(obj);
  }
 }
 
 public static <T> void printCollection2(Collection<T> collection){
  //collection.add(1);
  System.out.println(collection.size());
  for(Object obj : collection){
   System.out.println(obj);
  }

 }
 
 
 public static <T> void copy1(Collection<T> dest,T[] src){
  
 }
 
 public static <T> void copy2(T[] dest,T[] src){
  
 } 
}

</span>


这段代码是高薪技术中的:
通过反射获得泛型的实际类型参数的代码!
 Type getGenericReturnType()
返回表示由此 Method 对象所表示方法的正式返回类型的 Type 对象。
上面的这个方法就是在注释2的地方提到了关于method中的方法。
返回表示此method对象所表示方法的正式返回类型的Type对象。

如果返回类型是参数化类型,则返回的Type对象必须实际反映源代码中所有参数的实际类型。
如果返回类型是类型变量或参数化类型,则创建它。否则将解析它。
getGenericParameterTypes
public Type[] getGenericParameterTypes()按照声明顺序返回 Type 对象的数组,这些对象描述了此 Method 对象所表示的方法的形参类型的。如果底层方法不带参数,则返回长度为 0 的数组。
如果形参类型是参数化类型,则为其返回的 Type 对象必须实际反映源代码中使用的实际类型参数。
如果形参类型是类型变量或参数化类型,则创建它。否则将解析它。


 

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值