public class Product<T, M> { // 멀티 타입 파라미터
private T kind;
private M model;
public T getKind() { return this.kind; }
public M getModel() { return this.model; }
public void setKind(T kind) { this.kind = kind; }
public void setModel(M model) { this.model = model;}
}
실제 타입지정용 클래스 2개 Car,Tv
public class Car {}
public class Tv {}
실행클래스
public class ProductExample {
public static void main(String[] args) {
Product<Tv, String> product1 = new Product<>(); // 자바 5이후부터 뒷부분 파라미터타입 생략가능
product1.setKind(new Tv());
product1.setModel("스마트TV");
Tv tv = product1.getKind();
String tvModel = product1.getModel();
Product<Car, String> product2 = new Product<Car, String>(); // 타입 파라미터 o
product2.setKind(new Car());
product2.setModel("디젤");
Car car = product2.getKind();
String carModel = product2.getModel();
}
}
'JAVA' 카테고리의 다른 글
제네릭 메소드의 멀티 파라미터타입을 사용한 비교 (0) | 2022.10.12 |
---|---|
제네릭 메소드 (0) | 2022.10.12 |
컬렉션 프레임웤 TreeMap (0) | 2022.10.11 |
컬렉션 프레임웤 Hashtable (0) | 2022.10.11 |
컬렉션 프레임웤 HashMap (1) | 2022.10.11 |