한 번에 끝내는 Java/Spring 웹 개발 마스터
- Generic
package ch09;
public class Point<T, V> {
// 2개의 점이 어떤 타입이든 replace가 가능하다.
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;
}
}
package ch09;
public class GenericMethod {
public static <T, V> double makeRectangle(Point <T,V> p1, Point <T,V> p2) {
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 (0, 0.0);
Point<Integer, Double> p2 = new Point (10, 10.0);
double size = GenericMethod.<Integer, Double>makeRectangle(p1,p2);
System.out.print(size);
}
}
Generic Method 2번째