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);
}