public class mainClass {
public static void main(String[] args) {
/*
Generic == template(형태)
: 자료형의 변수
: 같은 클래스에서 다양한 자료형을 사용하고 싶은 경우
*/
//Box<int> box = new Box<>(123);//일반 자료형으로 적으면 에러남 ex.char/int ....
Box<Integer> box = new Box<Integer>(111);
System.out.println(box.getTemp());//답 : 111
System.out.println(box.getTemp()+1);//답 : 112
Box<Object> box1 = new Box<Object>(111);//자료형에 Object도 사용 가능
System.out.println(box1);//입력값이없어서 주소값나옴
Box<String> sBox = new Box<String>("my world");
System.out.println(sBox.getTemp());
BoxMap<Integer, String> bmap = new BoxMap<Integer, String>(1001, "hello");
System.out.println(bmap.getKey());
System.out.println(bmap.getValue());
}
}
class Box<T>{ //<T> : Generic
T temp;
public Box(T temp) {
this.temp = temp;
}
public T getTemp() {//T로 적은 부분은 자료형을 적던 부분
return temp;
}
public void setTemp(T temp) {
this.temp = temp;
}
}
class BoxMap<K, Value>{//제네릭은 여러개 집어넣을 수 있고 단어 알파벳 상관없고 , 로 구분한다
K key;
Value value;
public BoxMap(K key, Value value) {
super();
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
}