타입 변수의 제한
제네릭은 T 와 같은 타입 변수를 사용해 타입을 제한합니다.
이 때 extends 키워드를 사용하면 타입 변수에 특정 타입만을 사용하도록 제한할 수 있습니다.
class AnimalList<T extends LandAnimal> {...}
클래스의 타입 변수에 제한을 걸어 놓으면 클래스 내부에서 사용된 모든 타입 변수에 제한이 걸립니다.
클래스가 아닌 인터페이스를 구현할 때에도 implements 가 아닌 extends를 사용해야 합니다.
interface WarmBlood {...}
class AnimalList<T extends WarmBlood> {...} // implements 키워드를 사용하면 안된다.
클래스와 인터페이스를 동시에 상속받고 구현해야 한다면 &(엠퍼센트)를 사용하면 됩니다.
class AnimalList<T extends LandAnimal & WarmBlood> {...}
예제
public class 제네릭 {
public static void main(String[] args) {
AnimalList<LandAnimal> landAnimal = new AnimalList<>(); // Java SE 7부터 생략 가능
landAnimal.add(new LandAnimal());
landAnimal.add(new Cat());
landAnimal.add(new Dog());
for(int i = 0; i < landAnimal.size(); i++)
{
landAnimal.get(i).crying();
}
}
}
class LandAnimal
{
public void crying()
{
System.out.println("육지동물");
}
}
class Cat extends LandAnimal
{
public void crying()
{
System.out.println("냐옹냐옹");
}
}
class Dog extends LandAnimal
{
public void crying()
{
System.out.println("멍멍");
}
}
class Sparrow
{
public void crying()
{
System.out.println("짹짹");
}
}
class AnimalList<T extends LandAnimal>
{
ArrayList<T> al = new ArrayList<T>();
void add(T animal)
{
al.add(animal);
}
T get(int index)
{
return al.get(index);
}
boolean remove(T animal)
{
return al.remove(animal);
}
int size()
{
return al.size();
}
}
실행결과
제네릭 메소드 (generic method)
제네릭 메소드란 메소드의 선언부에 타입 변수를 사용한 메소드를 의미합니다.
이 때 타입 변수의 선언은 메소드 선언부에서 반환 타입 바로 앞에 위치합니다.
public static <T> void sort(...) {...}
다음 예제의 제네릭 클래스에서 정의된 타입 변수 T와 제네릭 메소드에서 사용된 타입 변수 T는 전혀 별개의 것임을 주의해야 합니다.
class AnimalList<T> {
public static <T> void sort(List<T> list, Comparator<? super T> comp) {
}
}
와일드카드의 사용
와일드카드(wild card)란 이름에 제한에 두지 않음을 표현하는데 사용되는 기호를 의미합니다.
자바의 제네릭에서는 물음표(?) 기호를 사용해 와일드카드를 사용합니다.
<?> // 타입 변수에 모든 타입을 사용할 수 있음.
<? extends T> // T 타입과 T 타입을 상속받는 자손 클래스 타입만을 사용할 수 있음
<? super T> // T 타입과 T 타입이 상속받은 조상 클래스 타입만을 사용할 수 있음
예시
import java.util.*;
public class 제네릭 {
public static void main(String[] args) {
AnimalList<Cat> catList = new AnimalList<>();
catList.add(new Cat());
AnimalList<Dog> dogList = new AnimalList<>();
dogList.add(new Dog());
AnimalList.cryingAnimalList(catList);
AnimalList.cryingAnimalList(dogList);
}
}
class LandAnimal
{
public void crying()
{
System.out.println("육지동물");
}
}
class Cat extends LandAnimal
{
public void crying()
{
System.out.println("냐옹냐옹");
}
}
class Dog extends LandAnimal
{
public void crying()
{
System.out.println("멍멍");
}
}
class Sparrow
{
public void crying()
{
System.out.println("짹짹");
}
}
class AnimalList<T extends LandAnimal>
{
ArrayList<T> al = new ArrayList<T>();
public static void cryingAnimalList(AnimalList<? extends LandAnimal> al) {
LandAnimal la = al.get(0);
la.crying();
}
void add(T animal)
{
al.add(animal);
}
T get(int index)
{
return al.get(index);
}
boolean remove(T animal)
{
return al.remove(animal);
}
int size()
{
return al.size();
}
}
실행화면