제네릭 타입은 두 개 이상의 타입 파라미터 사용 가능
ex) class<K, V> {}
Key, Value
package sec03_exam01_multi_type_parameter;
public class Car {
}
package sec03_exam01_multi_type_parameter;
public class Tv {
}
package sec03_exam01_multi_type_parameter;
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; }
}
package sec03_exam01_multi_type_parameter;
public class ProductExample {
public static void main(String[] args) {
Product<Tv, String> product1 = new Product<Tv, String>();
product1.setKind(new Tv());
product1.setModel("스마트Tv");
Tv tv = product1.getKind();
String tvModel = product1.getModel();
Product<Car, String> product2 = new Product<Car, String>();
product2.setKind(new Car());
product2.setModel("디젤");
Car car = product2.getKind();
String carModel = product2.getModel();
}
}
'JAVA' 카테고리의 다른 글
제네릭 메서드 예제 (태그 설명) (0) | 2022.09.29 |
---|---|
제네릭 메서드 (0) | 2022.09.29 |
제네릭 타입 (0) | 2022.09.29 |
자료구조 Queue 예제 (1) | 2022.09.29 |
컬렉션 프레임웍 List (0) | 2022.09.27 |