1.泛型的通配符 格式:<? extends E>表传递E的子类和E本身 <? super E> 表传递E的父类和E本身 通配符主要起限定作用
2.泛型不能继承 数据可以继承 使用通配符可以继承
分析从下到上 写代码从上到下
public class animal {
private String name;
private int age;
public animal() {
}
public animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void eat(){
System.out.println("吃东西");
}
}
public class cat extends animal{
}
public class bsm extends cat{
@Override
public void eat() {
System.out.println("一只叫做"+getName()+","+getAge()+"岁的波斯猫在吃小饼干");
}
}
public class lhm extends cat{
@Override
public void eat() {
System.out.println("一只叫做"+getName()+","+getAge()+"岁的狸花猫在吃鱼");
}
}
public class dog extends animal{
}
public class hsq extends dog{
@Override
public void eat() {
System.out.println("一只叫做"+getName()+","+getAge()+"岁的哈士奇在吃东西柴家");
}
}
public class td extends dog{
@Override
public void eat() {
System.out.println("一只叫做"+getName()+","+getAge()+"岁的泰迪在蹭加吃");
}
}
import java.util.ArrayList;
public class keeppet {
public static void main(String[] args) {
ArrayList<lhm> list=new ArrayList();
ArrayList<hsq>list1=new ArrayList<>();
ArrayList<bsm>list2=new ArrayList<>();
ArrayList<td>list3=new ArrayList<>();
keeppet1(list1);
keeppet1(list);
keeppet1(list2);
keeppet1(list3);
}
/*
//要求一 只养猫不能养狗
public static void keeppet1(ArrayList<? extends cat> list){
}
//要求二 只养狗不能养猫
public static void keeppet1(ArrayList<? extends dog> list){
}
//要求三 能养所有动物 其他类型不能
public static void keeppet1(ArrayList<? extends animal> list){
}
*/
}