Java泛型,?通配符与Object

/** 
 * JDK1.5 泛型 
 *  
 * @author  
 *  
 */  
public class GenericStudy {  
  
    @SuppressWarnings( { "unchecked", "unused" })  
    public static void main(String[] args) throws Exception {  
        // 第1点:Java的泛型仅仅只是通用Java的编译器实现的,进行全安检查,编译成class文件后没有保留泛型信息  
        Collection<Integer> c1 = new ArrayList<Integer>();  
        Collection<String> c2 = new ArrayList<String>();  
        // 1.1 获取c1与c2的字节码看是否相同  
        System.out.println("c1与c2的字节码是否相同:" + (c1.getClass() == c2.getClass()));// true  
        c1.add(23);  
        // 1.2 通用反射我们可以向c1中添加String类型的数据(class文件没有任何泛型)  
        Class clazz = c1.getClass();  
        Method collAddMethod = clazz.getMethod("add", Object.class);  
        collAddMethod.invoke(c1, "http://zmx.iteye.com");  
        for (Object obj : c1) {  
            System.out.println(obj);  
        }  
  
        // 泛型中的?通配符:表示任何类型,它与Object是有区别的如下所示:  
        // collection1可以存放任保类型,而collection2则只能放在Object类型  
        Collection<?> collection1 = new ArrayList<String>();  
        collection1 = new ArrayList<Integer>();  
        collection1 = new ArrayList<Object>();  
        Collection<Object> collection2 = new ArrayList<Object>();  
        // 泛型中的向上或向下限定  
        // collection3表示它可以存放Number或Number的子类  
        Collection<? extends Number> collection3 = null;  
        collection3 = new ArrayList<Number>();  
        collection3 = new ArrayList<Double>();  
        collection3 = new ArrayList<Long>();  
        // collection4表示它可以存放Integer或Integer的父类  
        Collection<? super Integer> collection4 = null;  
        collection4 = new ArrayList<Object>();  
  
        // 泛型简单使用(Map.Entry是Map的一个内部类,表法Map中存在的一个对象)  
        Map<String, Integer> testMap = new HashMap<String, Integer>();  
        testMap.put("xiaobojava", 2);  
        testMap.put("mengya", 3);  
        testMap.put("zmx", 6);  
        Set<Map.Entry<String, Integer>> entrySet = testMap.entrySet();  
        for (Map.Entry<String, Integer> entry : entrySet) {  
            System.out.println(entry.getKey() + ":" + entry.getValue());  
        }  
  
        // 自定义泛型  
        String[] a = { "aa", "bb", "cc" };  
        swap(a, 1, 2);  
        Integer[] b = { 32, 45, 67 };  
        swap(b, 1, 2);  
        // 泛型中的类型不能为基本数据类型(如下就不行)  
        int[] c = { 1, 3, 5 };  
        // swap(c,1,2);  
  
        Vector<Date> v1 = new Vector<Date>();  
        // 由于泛型是由编译器实现的语法检查,编译成class文件后没有保留泛型信息,但可以根据Method获取  
        Method applyMethod = GenericStudy.class.getMethod("applyVector",  
                Vector.class);  
        // getGenericParameterTypes()按照声明顺序返回 Type 对象的数组,这些对象描述了此 Method  
        // 对象所表示的方法的形参类型的。  
        Type[] type = applyMethod.getGenericParameterTypes();  
        // ParameterizedType extends Type  
        ParameterizedType pType = (ParameterizedType) type[0];// applyVector只用一个参数  
        // getRawType()返回 Type 对象,表示声明此类型的类或接口  
        System.out  
                .println("getRawType()方法:表示声明此类型的类或接口是:" + pType.getRawType());  
        // getActualTypeArguments()返回表示此类型实际类型参数的 Type 对象的数组。  
        System.out.println("getActualTypeArguments()方法:实际类型参数的 Type 对象"  
                + pType.getActualTypeArguments()[0]);// Vector<Date>只用一个Date类型  
  
    }  
  
    public static void applyVector(Vector<Date> v1) {  
  
    }  
  
    public static void printCollection(Collection<?> collection) {  
        for (Object obj : collection) {  
            System.out.println(obj);  
        }  
    }  
  
    /** 
     * 自定义泛型一个泛型方法 
     *  
     * @param <T> 
     *            表示自己定义的一个泛型类型(T不能是基本数据类型) 
     * @param a 
     *            T类型的数组 
     * @param m 
     *            T类型的数组m位置 
     * @param n 
     *            T类型的数组n位置 
     */  
    public static <T> void swap(T[] a, int m, int n) {  
        T temp = a[n];  
        a[n] = a[m];  
        a[m] = temp;  
    }  
  
    /** 
     * 将某个类型的数组元素copy到这个类型集合 
     *  
     * @param <T> 
     *            表示自己定义的一个泛型类型(T不能是基本数据类型) 
     * @param a 
     *            集合 
     * @param b 
     *            数组 
     */  
    public <T> void copyArray2Collection(Collection<T> a, T[] b) {  
  
    }  
  
}  
/** 
 * 泛型DAO接口 
 *  
 * @author  
 *  
 */  
public interface GenericDAO<E> {  
    /** 
     * 持久化一个对象 
     *  
     * @param entity 
     *            实体对象 
     */  
    public void save(E entity);  
  
    /** 
     * 根据主键查询一个实体对象 
     *  
     * @param id 
     *            主键 
     * @return 
     */  
    public E queryByPk(Integer id);  
  
    /** 
     * 删除一个实体对象 
     *  
     * @param entity 
     */  
    public void delete(E entity);  
  
    /** 
     * 删除一个实体对象 
     *  
     * @param id 
     *            主键 
     */  
    public void delete(Integer id);  
  
    /** 
     * 修改一个实体对象 
     *  
     * @param entity 
     */  
    public void update(E entity);  
  
    /** 
     * 查询一个实体对象集合 
     *  
     * @return 
     */  
    public List<E> queryList();  
  
} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值