java泛型

好处

  1. 规定集合的类型,只能传入改泛型限定的类型,避免强转
  2. 多个类型,执行相同逻辑的代码,避免多次重载。

泛型接口

  1. 子类实现泛型接口,可以指定它也是泛型 class B<T> implement A<T>
  2. 子类实现泛型接口,直接implement A<String>

泛型方法

  1. 指定泛型方法接受的参数和返回值,public <T> T genericMethod(T t){}
  2. 泛型方法在调用的时候,可以省略<T>, a.<String>genericMethod(“A”);

限定类型变量

        extends 上界,<T extends Comparable>,代表T必须是Comparable的子类。可以指定多个类或者接口,如果是一个类,必须放在第一个<T extends ArrayList&Comparable>,且只有一个类;接口可以指定多个;上界对于get是安全的,不允许set,记忆法:eg-> extends get

继承关系:Food->Fruit->Apple->Hongfushi

    public static void main(String[] args) {
		GenericType<Food> foodGt = new GenericType<>();
		GenericType<Apple> appleGt = new GenericType<>();

//		print(foodGt); //编译错误,Food不是Fruit的子类
		print(appleGt);

//		appleGt.setData(new Food()); //编译错误 只能set apple的子类
		appleGt.setData(new Hongfushi()); //set ok Hongfushi是apple的子类

		Food gFood = foodGt.getData(); //拿到的是Food类型
	}

	public static void print(GenericType<? extends Fruit> g) {
	}

        supper 下界,<T super Apple>,代表T必须是Apple的父类。下界对于set是安全的,不允许get,记忆法:ss -> super set

继承关系:Food->Fruit->Apple->Hongfushi                         

    public static void main(String[] args) {
		GenericType<Apple> appleGt = new GenericType<>();
		GenericType<Hongfushi > hongGt = new GenericType<>();

		print(appleGt);
//		print(hongGt);	 //编译错误,Hongfushi不是Appble的父类

		Apple apple = new Apple();
		Fruit fruit = new Fruit();
		Hongfushi hongfushi = new Hongfushi();
		appleGt.setData(apple); //set ok
		appleGt.setData(hongfushi); //set ok 只能set Apple的子类
//		appleGt.setData(fruit);  //编译错误,fruit不是Apple的子类
		Apple gApple = appleGt.getData(); //可以获取到Apple类型
	}

	public static void print(GenericType<? super Apple> g){}

泛型中约束和局限性

  1. 不能实例化泛型变量:T data = new T()(错),可以通过反射newInstance()
  2. 静态方法不能使用泛型变量:public static T instance(错)
  3. 泛型只允许引用类型,不适用基本类型:List<int>(错)。如果使用想使用基本类型,需要装箱,如Integer,Long等
  4. 不能用instanceof判断泛型类型 if(A instanceof List<String>)(错),语法不通过
  5. 数组不能使用泛型初始化:Restrict<Double>[] a = new Restrict<Double>[](错),可以声明泛型数组,但是不能初始化!

虚拟机如何实现泛型

         虚拟机中,采用泛型擦除的方式。

  1. 对于private T t,不带限定词的,直接擦除为private Object t
  2. 上界 T extends Comparable&List,擦除后变为 Comparable t
  3. 下界 T super Apple,擦除后变为Apple a,
  4. 编译器会在合适的时机,加入强制类型转化,如Map<String,String> map; (String)map.get();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值