您的位置:首页 > 产品设计 > UI/UE

java <? super Fruit>与<? extends Fruit>

2016-02-25 08:59 423 查看
package Test2016;

import java.util.ArrayList;
import java.util.List;

public class Test2016 {

public static void main(String[] args) {

List<? super Fruit> first = new ArrayList<Fruit>();	//Fruit -> Apple

first.add(new Apple());
first.add(new Fruit());
first.add(new RedApple());

//		first.add(new Food());	//error

System.out.println(first.get(0));
System.out.println(first.get(1));

List<? extends Apple> second = new ArrayList<Apple>();

second.add(null);
}

}

class Food {
}

class Fruit extends Food {
}

class Apple extends Fruit {
}

class RedApple extends Apple {
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: