티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/86
public class GenericIdol<T> { // T extends 사용
// field
private T idolGroup; // T 타입의 변수 idolGroup
public T getIdolGroup() { // T 타입을 반환하는 제너릭 메소드
return idolGroup;
}
public void setIdolGroup(T idolGroup) {
this.idolGroup = idolGroup;
}
public String toString() {
return idolGroup.toString();
}
}
public class Aqours extends {
public String toString() {
return "Aqours 소속의 타입입니다.";
}
}
public class Liella extends IdolGroup {
public String toString() {
return "Liella 소속의 타입입니다.";
}
}
public class GenericIdolTest {
public static void main(String[] args) {
GenericIdol<Aqours> aqoursIdol = new GenericIdol<Aqours>(); // Aqours 자료형의 aqoursIdol으로 지정
aqoursIdol.setIdolGroup(new Aqours());
Aqours aqours = aqoursIdol.getIdolGroup(); // 형변환 하지 않음**
System.out.println(aqoursIdol);
GenericIdol<Liella> liellaIdol = new GenericIdol<Liella>(); // Liella 자료형의 liellaIdol으로 지정
liellaIdol.setIdolGroup(new Liella());
Liella liella = liellaIdol.getIdolGroup(); // 형변환 하지 않음**
System.out.println(liellaIdol);
}
}
ArrayList list = new ArrayList() => var list = new ArrayList();
public abstract class IdolGroup {
public abstract void posing();
}
public class Aqours extends IdolGroup {
public String toString() {
return "Aqours 소속의 타입입니다.";
}
public void posing() {
System.out.println("Aqours의 포즈를 취합니다.");
}
}
public class Liella extends IdolGroup {
public String toString() {
return "Liella 소속의 타입입니다.";
}
public void posing() {
System.out.println("Liella의 포즈를 취합니다.");
}
}
public class GenericIdolTest {
public static void main(String[] args) {
GenericIdol<Aqours> aqoursIdol = new GenericIdol<Aqours>(); // Aqours 자료형의 aqoursIdol으로 지정
aqoursIdol.setIdolGroup(new Aqours());
Aqours aqours = aqoursIdol.getIdolGroup(); // 형변환 하지 않음**
System.out.println(aqoursIdol);
aqours.posing();
GenericIdol<Liella> liellaIdol = new GenericIdol<Liella>(); // Liella 자료형의 liellaIdol으로 지정
liellaIdol.setIdolGroup(new Liella());
Liella liella = liellaIdol.getIdolGroup(); // 형변환 하지 않음**
System.out.println(liellaIdol);
liella.posing();
}
}
public <자료형 매개 변수> 반환형 메소드 이름(자료형 매개변수...) {}
public class Point<T, V> {
// field
T x;
V y;
Point(T x, V y) {
this.x = x;
this.y = y;
}
public T getX() {
return x;
}
public V getY() {
return y;
}
}
public class GenericMethod {
public static <T,V> double makeRectangle(Point<T,V> p1, Point<T,V> p2) {
// Point 점인 p1, p2인데 타입이 T,V인 것에 대해 makeRectangle
double left = ((Number)p1.getX()).doubleValue();
double right = ((Number)p2.getX()).doubleValue();
double top = ((Number)p1.getY()).doubleValue();
double bottom = ((Number)p2.getY()).doubleValue();
double width = right - left;
double height = bottom - top;
return width * height;
}
public static void main(String[] args) {
Point<Integer, Double> p1 = new Point<Integer, Double>(0, 0.0);
Point<Integer, Double> p2 = new Point<Integer, Double>(10, 10.0);
double size = GenericMethod.<Integer, Double>makeRectangle(p1, p2); // static 메소드이므로 GenericMethod.로 접근
System.out.println(size);
}
}