コンビネーションモードと汎用モード

9165 ワード

package p1;

public class B {
	public static void main(String[] args) {
		Holder1<? extends Fruit> h1 = new Holder1<Apple>();
		h1.set(new Fruit());  //     
		Fruit f1 = h1.get();
		
		Holder1<? super Fruit> h2 = new Holder1<Fruit>();
		h2.set(new Apple());
		h2.get(); //     Object  
		
		Holder1<Apple> h3 = new Holder1<Apple>();
		h3.set(new Apple());
		h3.get();
		
		Holder1<Orange> h4 = new Holder1<Orange>();
		h4.set(new Orange());
		h4.get();
		
		Holder2 h5 = new Holder2();
		h5.set(new Apple());
		h5.get();
}

//     
class Holder1<T> {
	private T value;
	
	public void set(T val) {
		value = val;
	}
	
	public T get() {
		return value;
	}
}

//     
class Holder2 {
	private Fruit value;
	
	public void set(Fruit val) {
		value = val;
	}
	
	//     (value     Fruit   )
	public Fruit get() {
		return value;
	}
}


class Fruit {
	
}

class Apple extends Fruit {
	
}

class Orange extends Fruit {
	
}

組み合わせモードと汎用モード、それぞれ千秋があって、いっしょに討論することを歓迎します