---------------------- android培训、java培训、期待与您交流! ----------------------
ArrayList <Integer> list2 = new ArrayList <Integer>();
//以下使用反射在使用泛型<Integer>的集合中中加入字符串
list2.getClass().getMethod("add", Object.class).invoke(list2, "abc");
System.out.println(list2.get(0));//在runtime时,不存在泛型实例
//参数化类型与原始类型的兼容性
Vector v1 = new Vector <String>();//原始类型可以引用一个参数化类型的对象
Vector <Object> v2 = v1;//参数化类型可以引用一个原始类型的对象(这里编译器把v1看作是一个原始类型)
//以下内容为类型推断
Integer x = add(3, 4);
Number x1 = add(3.5, 6);//float与int的类型交集为Number
Object x2 = add("a", 665);//String与int的类型交集为Object
private static <T> T add(T x,T y){//用于类型推断的方法
return y;
}
//运用泛型向任意类型的集合中添加,相应类型的数组内容
private static <T> void add(Collection <T> dest,T src[]){
for(T s : src){
dest.add(s);
}
}
//以下是通过反射获得泛型的的实际参数类型
Method method = GenericTest.class.getMethod("applyGenericDao", GenericDao.class);
Type[] types = method.getGenericParameterTypes();//Method类提供取得泛型形参类型数组
ParameterizedType pType = (ParameterizedType)types[0];//ParameterizedType是Type的一个子类
System.out.println(pType.getRawType());//运用ParameterizedType的方法返回此类型的类
System.out.println(pType.getActualTypeArguments()[0]);//返回实际类型参数的Type对象
package HEIMA_annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import HEIMA_refelct.ReflectPoint;
public class GenericTest {
public static void main(String[] args) throws SecurityException, NoSuchMethodException,
IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
ArrayList list1 = new ArrayList ();//没有使用泛型,编译出现警告
list1.add(1);
list1.add("d");
ArrayList<String> list = new ArrayList <String>() ;
// list.add(1);
list.add("a");
// list.add(1L);
String i = list.get(0);
//new String (new StringBuffer("abc"))反射中使用到泛型
Constructor<String> constructor = String.class.getConstructor(StringBuffer.class);
String str = constructor.newInstance(new StringBuffer("abc"));
System.out.println(str);
ArrayList <Integer> list2 = new ArrayList <Integer>();
//true;字节码相同,说明泛型是提供给编译器使用的,在runtime时没有泛型
System.out.println(list.getClass() == list2.getClass());
//list2.add("abc");编译不通过,因为使用了泛型
//以下使用反射在使用泛型<Integer>中加入字符串
list2.getClass().getMethod("add", Object.class).invoke(list2, "abc");
System.out.println(list2.get(0));
//参数化类型与原始类型的兼容性
Vector v1 = new Vector <String>();//原始类型可以引用一个参数化类型的对象
Vector <Object> v2 = v1;//参数化类型可以引用一个原始类型的对象(这里编译器把v1看作是一个原始类型)
HashMap <String, Integer> maps = new HashMap <String, Integer>();
maps.put("zxx", 4);
maps.put("lhm", 2);
maps.put("zlx", 32);
Set <Map.Entry <String, Integer>> entrySet = maps.entrySet();//HashMap无法进行迭代,必须通过entrySet()方法先转化为Set
for(Map.Entry <String, Integer> entry : entrySet){
System.out.println(entry.getKey()+":"+entry.getValue());
}
//以下内容为类型推断
Integer x = add(3, 4);
Number x1 = add(3.5, 6);//float与int的类型交集为Number
Object x2 = add("a", 665);//String与int的类型交集为Object
swap(new String []{"abc","xyz","jkl"}, 0, 2);
// swap(new int[]{1,2,3}, 0, 2);//编译不通过,只有引用类型才能成为泛型的实际参数,int是基本类型
Object obj = "123";
String str1 = autoconvert(obj);//运用泛型转换,但是数据不对会报错
System.out.println(str1);
String fillArrayVal = "abc";//运用泛型,在任意类型的数组中,填入相应类型的对象
String [] b = new String[2];
fillArray(b, fillArrayVal);
copy1(new Vector <String>(), new String [10]);
copy2(new Date[10], new String [10]);//类型推断为Date和String的交集(Object)
// copy1(new Vector <Date>(), new String [10]);//编译不通过,前面指定Date,泛型具有传递性,后面也应为Date
//以下为自定义泛型类的应用
GenericDao<ReflectPoint> dao = new GenericDao <ReflectPoint>();
dao.add(new ReflectPoint(3,2));//GenericDao类定义了泛型,这里add方法的参数只能是ReflectPoint对象!
// String string = dao.findById(1);//编译不通过,定义了泛型,返回类型只能是ReflectPoint类型
//以下是通过反射获得泛型的的实际参数类型
Method method = GenericTest.class.getMethod("applyGenericDao", GenericDao.class);
Type[] types = method.getGenericParameterTypes();//Method类提供取得泛型形参类型数组
ParameterizedType pType = (ParameterizedType)types[0];//ParameterizedType是Type的一个子类
System.out.println(pType.getRawType());//运用ParameterizedType的方法返回此类型的类
System.out.println(pType.getActualTypeArguments()[0]);//返回实际类型参数的Type对象
}
public static void applyGenericDao(GenericDao<ReflectPoint> dao){
}
private static <T> void fillArray(T[] a,T obj){//运用泛型,在任意类型的数组中,填入相应类型的对象
for(T a1 : a){
a1 = obj;
System.out.println(a1);
}
}
private static <T> T autoconvert(Object obj){//运用泛型自动将Object对象转换为其他对象(类型错误也会报强制转换错)
return (T)obj;
}
private static <T> void swap(T a[],int i,int j){//转换数组中任意2个元素的位置
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static <T> T add(T x,T y){//用于类型推断的方法
return y;
}
private static void printCollection(Collection <?> collection,int i ){//运用通配符,打印任意类型的集合
System.out.println(collection.size());
for(Object arg : collection){
System.out.println(arg);
}
// collection.add(i);使用通配符后不能使用add方法
}
private static <T> void printCollection2(Collection <T> collection,T obj){//运用泛型,打印任意类型的集合(使用通配符更有效)
System.out.println(collection.size());
for(Object arg : collection){
System.out.println(arg);
}
collection.add(obj);//使用泛型后可以使用add方法!
}
private static <T> void copy1(Collection <T> dest,T src[]){//运用泛型向任意类型的集合中添加,相应类型的数组内容
for(T s : src){
dest.add(s);
}
}
private static <T> void copy2(T []dest,T src[]){//相同类型的数组传值
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima