Java Generic

자바 Generic 기본 예제입니다.

package tue160607; 
 
/** 
 * Created on 2016-06-07. 
 */ 
public class GenericBaseFruitBox { 
    public static void main(String[] args) { 
        FruitBox<Orange> orBox = new FruitBox<Orange>(); 
        orBox.store(new Orange(10)); 
        Orange org = orBox.pullOut(); 
        org.showSugarContent(); 
 
        FruitBox<Apple> apBox = new FruitBox<Apple>(); 
        apBox.store(new Apple(20)); 
        Apple app = apBox.pullOut(); 
        app.showAppleWeight(); 
    } 
} 
 
class Orange  { 
    private int sugarContent; 
    protected Orange(int sugar) { 
        sugarContent = sugar; 
    } 
    protected void showSugarContent() { 
        System.out.println("당도 " + sugarContent); 
    } 
} 
 
class Apple { 
    private int weight; 
    protected Apple(int weight) { 
        this.weight = weight; 
    } 
    protected void showAppleWeight() { 
        System.out.println("무게 " + weight); 
    } 
} 
 
class FruitBox<T> { 
    T item; 
    protected void store(T item) { 
        this.item = item; 
    } 
    protected T pullOut() { 
        return item; 
    } 
}