package Class;
import java.util.ArrayList;
import java.util.List;
/*
* extends继承:可理解为泛型子类的上限
*
*
*/
public class ExtendsTest {
public static void main(String[] args) {
//Fruit为上限,(向下兼容)
test<Fruit> text=new test<Fruit>();
test<Apple> text1=new test<Apple>();
test<Pear> text2=new test<Pear>();
//fujiApple的父类为Apple,而Fruit为fujiApple父类的父类,又因为其向下兼容,所以T的位置可为fujiApple。
test<fujiApple> text3=new test<fujiApple>();
//Fruit为上限,(向下兼容)
List<? extends Fruit> list=new ArrayList<Fruit>();
Text(list);
List<? extends Fruit> list1=new ArrayList<Apple>();
Text(list1);
List<? extends Fruit> list2=new ArrayList<Pear>();
Text(list2);
List<? extends Fruit> list3=new ArrayList<fujiApple>();
Text(list3);
//但以下不行,Object超出Fruit范围
/* List<?> list4=new ArrayList<Object>();
Text(list4);
List<? extends Object> list5=new ArrayList<Object>();
Text(list5);
*/
}
static void Text(List<? extends Fruit> list) {
//list.add(new Fruit());不能用在添加数据上,只能用在读取数据上
}
//泛型类,Fruit为T的上限
static class test<T extends Fruit> {
}
}
package Class;
/*
* 继承链图
* object
* |
* Fruit
* / \
* Apple Pear
* |
*fujiApple
*/
public class Fruit {
}
class Apple extends Fruit{
}
class Pear extends Fruit{
}
class fujiApple extends Apple{
}