출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]

1.

package generic;

public class Apple {

	private int weight; // 사과의 무게
	
	Apple(int weight){
		this.weight= weight;
	}
	
	public void showWeight() {
		System.out.println("사과의 무게 : " + weight);
	}
}
package generic;

public class Orange {

	private int sugarContent; // 오렌지 당도
	
	Orange(int sugar){
		this.sugarContent=sugar;
	}
	
	public void showsugarContent() {
		System.out.println("오렌지의 당도 : " + sugarContent);
	}
}
package generic;

public class AppleBox {
	
	Apple apple;
	
	public void store(Apple apple) {
		this.apple=apple;
	}
	
	public Apple PullOut() {
		return apple;
	}
}
package generic;

public class OrangeBox {
	
	Orange orange;
	
	public void store(Orange orange) {
		this.orange=orange;
	}
	
	public Orange PullOut() {
		return orange;
	}
}
package generic;

public class FruitBoxMain {

	public static void main(String[] args) {

		AppleBox appleBox = new AppleBox();
		appleBox.store(new Apple(10));
		Apple apple = appleBox.PullOut();
		
		apple.showWeight();
		
		OrangeBox orangeBox = new OrangeBox();
		// orangeBox.store(new String("오렌지"));
		orangeBox.store(new Orange(100));
		Orange orange = orangeBox.PullOut();
		
		orange.showsugarContent();
	}

}

2. 

fruitBox를 만들어서 사과와 오렌지를 각각 집어넣어줄 수 있지만,

아무거나 들어가도 빨간줄이 생기지 않지만, 실행하면 에러가 난다.

package generic;

public class FruitBox {
	
	Object fruit;
	
	public void store(Object fruit) {
		this.fruit=fruit;
	}
	
	public Object PullOut() {
		return fruit;
	}

}

 

package generic;

public class FruitBoxMain {

	public static void main(String[] args) {

		
		FruitBox box1 = new FruitBox();
		box1.store(new Apple(100));
		Apple apple = (Apple)box1.PullOut();
		apple.showWeight();
		
		FruitBox box2 = new FruitBox();
		box2.store("Apple"); // Apple 타입만 들어가야 한다. (아무거나 들어가도 ㅇㅋ인게 문제점..)
		Orange orange = (Orange)box2.PullOut();//형변환
		orange.showsugarContent();
		
	}

}

3.

제네릭을 이용해 FuritBox<T>를 생성해주면

형변환도 필요없고 알아서 올바르지 않은 타입이 들어가면 빨간줄이 생긴다.

package generic;

public class FruitBox <T> { // T > Apple / Orange
	
	T fruit; // Apple fruit
	
	public void store(T fruit) { // store(Apple fruit)
		this.fruit=fruit;
	}
	
	public T PullOut() { // public Apple PullOut()
		return fruit;
	}

}
package generic;

public class FruitBoxMain {

	public static void main(String[] args) {

		
		FruitBox<Apple> box1 = new FruitBox<Apple>();
		// box1.store("Apple"); 에러남
		box1.store(new Apple(10));
		Apple apple = box1.PullOut();
		apple.showWeight();
		
		FruitBox<Orange> box2 = new FruitBox<Orange>();
		// box2.store(new Apple(10)); // Orange만 들어갈 수 있게 됨
		box2.store(new Orange(100));		
		Orange orange = box2.PullOut(); //형변환도필요없다.
		orange.showsugarContent();
		
	}

}

4.

FruitBox<T>에 초기화 생성자를 추가하면 더욱 편리하다.

package generic;

public class FruitBox<T> { // T > Apple / Orange
	
	T fruit; // Apple fruit
	
	FruitBox(T fruit){   // FruitBox(Apple fruit){
		this.fruit=fruit;
		
	}
	
	public void store(T fruit) { // store(Apple fruit)
		this.fruit=fruit;
	}
	
	public T PullOut() { // public Apple PullOut()
		return fruit;
	}

}
package generic;

public class FruitBoxMain {

	public static void main(String[] args) {

		
		FruitBox<Apple> box1 = new FruitBox<Apple>(new Apple(10));
		Apple apple = box1.PullOut();
		apple.showWeight();
		
		FruitBox<Orange> box2 = new FruitBox<Orange>(new Orange(100));
		Orange orange = box2.PullOut(); //형변환도필요없다.
		orange.showsugarContent();
		
	}

}

+ Recent posts