public class Test {
public static void main(String [] args){
Plate<? extends Fruit> p = new Plate<Apple>(new Apple());
Food food = p.get();
Fruit fruit = p.get(); //ERROR
// p.set(new Fruit());
// p.set(new Orange());
// p.set(new Apple());
//error
// Apple apple = p.get();
Plate<? super Fruit> p1 = new Plate<Fruit>(new Fruit());
p1.set(new Apple());
p1.set(new Orange());
p1.set(new Fruit());
Object object = p1.get(); }
}
class Plate<T>{
private T item;
public Plate(T t){
item = t;
}
public T get() {
return item;
}
public void set(T item) {
this.item = item;
}
} //父类
class Food {
void eatFood(){
System.out.println("eatFood");
}
}
//子类1
class Fruit extends Food{
void eatFruit(){
System.out.println("eatFruit");
}
}
class Meat extends Food{
void eatMeat(){
System.out.println("eatMeat");
}
}
//子类2
class Apple extends Fruit{
void eatApple(){
System.out.println("eatApple");
}
}
class Orange extends Fruit{
void eatOrange(){
System.out.println("eatOrange");
}
}
class Pork extends Meat{
void eatPork(){
System.out.println("eatPork");
}
} class Beaf extends Meat{
void eatBeaf(){
System.out.println("eatBeaf");
}
}

<? extends Fruit> 相当于是什么意思? 就是 ? extends Fruit 里面是Fruit类或者他的子类但是具体什么类型不知道
所以可能是Fruit 可能是Apple可能是Orange 这里面相当于标记了一个占位符:CAP#1 但是不能插入他的子类了,
<?> 不写默认是<? extends Object>
所以取得时候只能取父类的类型 向下转型嘛 父类new子类对象可以
所以这种写法适合去取 因为set会失效
Plate相当于一个容器 <?>与<T>不同的是 <T>是可以已知的
<? extends Fruit> 规定了他的上界限是 Fruit 但是下面的类型不能确定
所以我不能存,因为如果我的下面的类型细度比较大,存进去的时候就会有信息丢失 ,但是我取的时候可以取他父类的类型这样就没有丢失
相反的可以这样 <? super Fruit> 这里面存的是Fruit的父类 那么就不能插入Fruit的父类了 但是向下转型只要是子类都是可以
放进去,粒度比这个小都可以放进去
取得时候只能取到<? super Fruit> 相当与是没有上限的父类 那就是Object类

最新文章

  1. Atitit 软件国际化原理与概论
  2. 头显HTC Vive北美直降100美元,中国区降价活动今日公布
  3. String.prototype.substr()
  4. 使用FindFirstFile,FindNextFile遍历一个文件夹
  5. nyoj 86 找球号(一)
  6. 算法导论学习-prim算法
  7. Windows远程桌面连接Ubuntu 14.04 (转)
  8. php 5.5 xhprof for windows
  9. mui开发app之webview是什么
  10. numpy中linspace用法 (等差数列创建函数)
  11. Python IDLE背景主题
  12. 自学python之路(day1)
  13. Numpy 数据类型
  14. ubuntu中minicom安装和使用
  15. 完美解决distinct中使用多个字段的方法
  16. session存入redis
  17. [osg][原]自定义osgGA漫游器
  18. tomcat运行监控脚本,自动启动
  19. Cucumber java + Webdriver(一)
  20. OBS显示器获取显示黑色没有图像

热门文章

  1. [转] Actor生命周期理解
  2. .NET 知识点总结
  3. 完美原创:centos7.1 从源码升级安装Python3.5.2
  4. 开发者必备的12个JavaScript库
  5. xampp使用技巧及问题汇总
  6. (转)DB2和 Oracle的并发控制(锁)比较
  7. 在eclipse中启动Tomcat报端口被占用的错误
  8. Javac的命令(-Xlint)
  9. 使用SQL语句创建数据库1
  10. spring mongodb查询