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

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 {
}

 

posted @ 2016-02-25 08:59  Answer.AI.L  阅读(650)  评论(0编辑  收藏  举报