java泛型1(泛型上限下限嵌套)

extends:泛型的上限<=  即子类

1.一般用于限制操作

2.不能使用者添加数据操作上,一般都是读取操作

3.规则

List<Fruit>---->List<? extends Fruit>

List<Apple>---->List<? extends Fruit>

List<? extends Apple>------><? extends Fruit>

不能存放

List<?>因为它等同于List<? extneds Object>

super:泛型的下限>=  即父类

1.一般用于限制操作

2.能使用者添加数据操作上,不能添加父对象

3.规则

List<Fruit>---->List<? extends Apple>

List<Apple>---->List<? extends Apple>

List<? super Fruit>------><? super Apple>

不能存放

List<?>因为它等同于List<? extneds Object>

泛型没有多态

没有泛型数组

jdk1.7泛型的简化

List<Furit> list=new ArrayList<>();

?表示类型不定,用作声明变量上

泛型嵌套

class Student<T>{
	T Score;

	public T getScore() {
		return Score;
	}

	public void setScore(T score) {
		Score = score;
	}
}
class Bjsxt<T>{
	T stu;

	public T getStu() {
		return stu;
	}

	public void setStu(T stu) {
		this.stu = stu;
	}
}
public class test1 {

	public static void main(String[] args) {
		Student<String>stu=new Student<String>();
		stu.setScore("优秀");
		System.out.print(stu.getScore());
		System.out.println();
		Bjsxt<Student<String>> bjsxt=new Bjsxt<Student<String>>();
		bjsxt.setStu(stu);
		stu=bjsxt.getStu();
		String score=stu.getScore();
		System.out.print(score);

	}

}
Map泛型的嵌套应用

Map<String,String>map =new HashMap<String,String>();
		map.put("a","A");
		map.put("b","B");
		Set<Entry<String,String>> entrySet=map.entrySet();
		for(Entry<String,String> entry:entrySet){
			String key=entry.getKey();
			String value=entry.getValue();
			System.out.println(key+"----->"+value);
		}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值