泛型

泛型

泛型类注意事项

  • 泛型类,如果没有指定具体的数据类型,此时,操作类型是0bject
  • 泛型的类型参数只能是类类型,不能是基本数据类型
  • 泛型类型在逻辑上可以看成是多个不同的类型,但实际上都是相同类型

优点

1.提高代码的复用性
2.防止类型转换异常,提高代码安全性


public class appp {
    public static void main(String[] args) {
        //泛型在创建对象的时候,来指定操作的具体类型
        Generic<Integer> intGeneric = new Generic<>(1);
        Integer key1 = intGeneric.getKey();
        System.out.println("key1:" + key1);
        System.out.println("===================================");

        Generic<String> strGeneric = new Generic<>("abc");
        String key2 = strGeneric.getKey();
        System.out.println("key2:" + key2);
        System.out.println("===================================");

        //泛型在创建对象的时候,没有指定类型,按照object类型来操作
        Generic generic = new Generic("ABC");
        Object key3 =generic.getKey();
        System.out.println("key3:"+key3);
        System.out.println("===================================");
        //泛型类,不支持基本数据类型
        //Generic<int> strGeneric = new Generic<int>(100);


        //同一泛型类,根据不同的数据类型创建的对象,本质上是同一类型。
        System.out.println(strGeneric.getClass());
        System.out.println(intGeneric.getClass());
        System.out.println(strGeneric.getClass() ==intGeneric.getClass());
        System.out.println(intGeneric.getClass().hashCode());
        System.out.println(strGeneric.getClass().hashCode());
    }
}

public class Generic<T> {
    //由外部使用类的时候来指定的
    private T key;


    public Generic(T key) {
        this.key = key;
    }

    public T getKey() {
        return key;
    }

    public void setKey(T key) {
        this.key = key;
    }

    @Override
    public String toString() {
        return "Generic{" +
                "key=" + key +
                '}';
    }
}


public class appp {
    public static void main(String[] args) {
        //创建抽奖器对象,指定数据类型
        ProductGetter<String> strProductGetter = new ProductGetter<>();
        String[] strProducts = {"苹果", "雪梨", "葡萄", "香蕉"};
        //遍历奖品放进奖池
        for (int i = 0; i < strProducts.length; i++) {
            strProductGetter.addProduct(strProducts[i]);
        }
        //抽奖
        String product1 = strProductGetter.getProduct();
        System.out.println("奖品为:"+product1);
        System.out.println("===================================================");

        ProductGetter<Integer> intProductGetter = new ProductGetter<>();
        int[] intProducts = {100,5000,50,1000,};
        //遍历奖品放进奖池
        for (int i = 0; i < intProducts.length; i++) {
            intProductGetter.addProduct(intProducts[i]);
        }
        //抽奖
        int product2 = intProductGetter.getProduct();
        System.out.println("奖品为:"+product2);
        
        //泛型方法的调用,类型通过调用方法的时候指定
        ArrayList<String> productt = new ArrayList<>();
        productt.add("a");
        productt.add("b");
        productt.add("c");
        productt.add("d");
        String product3 = strProductGetter.getProduct(productt);
        System.out.println(product3);

    }

}


import java.util.ArrayList;
import java.util.Random;

public class ProductGetter<T> {
    Random random = new Random();
    private T product;

    //奖品池
    ArrayList<T> list = new ArrayList<>();

    //添加奖品
    public void addProduct(T t) {
        list.add(t);
    }

    //抽奖
    public T getProduct() {
        product = list.get(random.nextInt(list.size()));
        return product;
    }
 /**泛型方法
     *
     * @param list 参数
     * @param <E> 泛型标识,由调用方法指定
     * @return
     */
    public <E> E getProduct(ArrayList<E> list){
        int i=random.nextInt(list.size());
        System.out.println(i);
        return list.get(i);

    }
}

posted @   小幼虫虫  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示