< ? extends Class > 与 < T extends Class >


在Java中,? 代表通配符

不确定泛型的具体类型时,可以用 ? 代替

边界

< ? extends Student > 表示上界限限定,泛型参数只能是Student类及其子类

< ? super Student > 表示下界限限定,泛型参数只能是Student类及其父类

以上把 ? 改成 T 仍然是上下界限限定

区别

T 用于泛型类以及泛型方法的定义,如

//泛型类
class Demo<T extends ClassDemo>{
	...
}
//泛型方法
public <T extends ClassDemo> void get(T x) {
	...
}

?是用于类的声明或者是参数,如

public class GenericsTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	ArrayList<? extends Number> str ;//在声明类时使用,不清楚泛型的类型,可用?表示,再用具体的类型实例化
	str = new ArrayList<Integer>();
	//str = new ArrayList<Double>();
	
	GenericsTest a = new GenericsTest();
	List<Double> list = new ArrayList<Double>();
	a.set(list);//在调用参数时直接使用具体的实参
	}
	public  void set(List<? extends Number> list) {//在定义时泛型参数不确定使用?表示
		
	}
}

版权声明:本文参考CSDN博主「轩辕秋风泪」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_45925974/article/details/104806007


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM