[ch12] 지네릭스, 열거형, 어노테이션

[1] 지네릭스


[1.2] 지네릭 클래스의 선언


class Box<B> {
	T item;
	
	void setItem(T item) { this.item = item; }
	T getItem() { return item; }
}

⇒ Object를 모두 T로 바꿔 선언

import java.util.ArrayList;

class Fruit					{ public String toString() { return "Fruit"; } }
class Apple extends Fruit	{ public String toString() { return "Apple"; } }
class Grape extends Fruit	{ public String toString() { return "Grape"; } }
class Toy					{ public String toString() { return "Toy";	 } }

class FruitBoxEx1 {
	public static void main(String[] args) {
		Box<Fruit>	fruitBox 	= new Box<Fruit>();
		Box<Apple>	appleBox 	= new Box<Apple>();
//		Box<Grape>	grapeBox 	= new Box<Apple>();		// 에러. 타입 불일치
		Box<Toy>	toyBox		= new Box<Toy>();
		
		fruitBox.add(new Fruit());
		fruitBox.add(new Apple());						// OK. void add(Fruit item)
		
		appleBox.add(new Apple());
		appleBox.add(new Apple());
//		appleBox.add(new Toy());						// 에러. Box<Apple>에는 Apple만 담을 수 있음
		
		toyBox.add(new Toy());
//		toyBox.add(new Apple());						// 에러. Box<Toy>에는 Apple을 담을 수 없음
		
		System.out.println(fruitBox);
		System.out.println(appleBox);
		System.out.println(toyBox);
	}	// main
}

class Box<T> {
	ArrayList<T> list = new ArrayList<T>();
	void add(T item)		{ list.add(item);			}
	T get(int i)			{ return list.get(i);		}
	int size()				{ return list.size();		}
	public String toString() { return list.toString();	}
}