package generic;
public class GenericMethodTest2 {
public static <T> void printArray(T[] array) {
for(T element : array) {
System.out.printf("%s ", element);
}
System.out.println();
}
public static void main(String[] args) {
// T타입은 객체만 가능하기 때문
Integer[] iArray = {10,20,30,40,50};
Double[] dArray = {1.1,1.2,1.3,1.4,1.5};
Character[] cArray = {'K','O','R','E','A'};
printArray(iArray);
printArray(dArray);
printArray(cArray);
}
}
package generic;
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 rect = GenericMethod.<Integer, Double>makeRectangle(p1,p2);
System.out.println("두 점으로 만들어진 사각형의 넓이는 "+ rect + "입니다.");
}
}