// IntegerBox.java
public class IntegerBox {
private Integer value;
public void set(Integer value) {
this.value = value;
}
public Integer get(){
return this.value;
}
}
// StringBox.java
public class StringBox {
public String value;
public void set(String value) {
this.value = value;
}
public String get() {
return this.value;
}
}
// ObjectBox.java
public class ObjectBox {
private Object value;
public void set(Object value) {
this.value = value;
}
public Object get() {
return this.value;
}
}
public class GenericBox<T> {
private T value;
public void set(T value) {
this.value =value;
}
public T get() {
return value;
}
}
public static void main(String[] args) {
GenericBox<Integer> integerBox = new GenericBox<>();
integerBox.set(10);
// integerBox.set("문자100"); -> 문제 발생
Integer integer = integerBox.get();
System.out.println("integer = " + integer);
}
public void method1(String param){
System.out.println(param);
}
public static void main(String[] args){
String a = "test";
method1(a);
}
param 이라는 것은 method에서 매개변수라고 하고,String a 는 인수, 인자라고 불린다. GenericBox<T> 에서의 T가 타입 매개변수이고, GenericBox<Integer> 에서 Integer가 타입 인자라고 부른다.Generic(제너릭) : 일반적인 , 범용적이라는 영어 뜻으로 여러 타입을 범용적으로 사용할 수 있다.
Generic Type(제너릭 타입) : 타입 매개변수를 가지는 클래스, 인터페이스 등 모두 합쳐서 부르는 말