1. 제네릭 - Generic1
제네릭 적용
package generic.ex1;
public class GenericBox<T> {
private T value;
public void set(T value) {
this.value = value;
}
public T get() {
return value;
}
}package generic.ex1;
public class BoxMain3 {
public static void main(String[] args) {
GenericBox<Integer> integerGenericBox = new GenericBox<>();
integerGenericBox.set(10);
Integer integer = integerGenericBox.get();
System.out.println("integer = " + integer);
GenericBox<String> stringGenericBox = new GenericBox<>();
stringGenericBox.set("hello");
String string = stringGenericBox.get();
System.out.println("string = " + string);
}
}원하는 모든 타입 사용 가능
타입 추론
제네릭 용어와 관례
용어 정리
제네릭 명명 관례
제네릭 기타
다음과 같이 한번에 여러 타입 매개변수를 선언할 수 있다.
타입 인자로 기본형은 사용할 수 없다.
때문에, 제네릭의 타입 인자로 기본형( int, double ..)은 사용할 수 없다. 대신에 래퍼 클래스( Integer , Double)를 사용 하면 된다.
int, double ..)은 사용할 수 없다. 대신에 래퍼 클래스( Integer , Double)를 사용 하면 된다.Last updated