java 高新技术【9.2】 泛型类以及反射在泛型中的应用

package com.my.Util; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Collection; import java.util.Date; import java.util.Vector; public class GenericTest { /** * @param args */ public static void main(String[] args) throws Exception { //Class<Number> x = String.class.asSubclass(Number.class); Class<?> y; Class<String> x ;//Class.forName("java.lang.String"); 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); /************************/ /*** * * 利用 反射 看 applyVector * webservice 要是说:有一个方法返回集合,这个集合没有指定具体的类型,那我就不知道要把数据转换成什么对象 * * 但是如果 你在定义的时候,指定了类型,我就知道要把数据 转换成 什么类型了。。。 * * 假如 set<E> 这里不用泛型,而到数据库查数据出来 就不知道 要把数据转换成什么类型的集合。 * * */ //Vector<Date> v1 = new Vector<Date>(); Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class); // 得到 泛型的参数类型,列表。 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){ // 通过变量自己 是没有办法 知道自己的实际类型的。。。 } 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){ } }
package com.my.Util; import java.util.Set; //dao data access object--->crud public class GenericDao<E> { public void add(E x){ } public E findById(int id){ return null; } public void delete(E obj){ } public void delete(int id){ } public void update(E obj){ } public static <E> void update2(E obj){ } public E findByUserName(String name){ return null; } public Set<E> findByConditions(String where){ return null; } }
package com.my.Util; import java.util.Date; public class ReflectPoint { private Date birthday = new Date(); private int x; public int y; public String str1 = "ball"; public String str2 = "basketball"; public String str3 = "itcast"; public ReflectPoint(int x, int y) { super(); this.x = x; this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final ReflectPoint other = (ReflectPoint) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } @Override public String toString(){ return str1 + ":" + str2 + ":" + str3; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }


更多泛型详细讲解:http://blog.youkuaiyun.com/itm_hadf/article/details/7411712


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值