JAVA
멀티 타입 파라미터
연93
2022. 10. 12. 10:00
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();
}
}