제네릭 메서드 활용

    제네릭 메서드란?

    • 자료형 매개변수를 메서드의 매개변수나 반환 값으로 가지는 메서드는
    • 자료형 매개 변수가 하나 이상인 경우도 있음
    • 제네릭 클래스가 아니어도 내부에 제네릭 메서드는 구현하여 사용 할 수 있음
    • public <자료형 매개 변수> 반환형 메서드 이름(자료형 매개변수.....) { }

    제네릭 메서드의 활용 예

    • 두 점(top, bottom)을 기준으로 사각형을 만들 때 사각형의 너비를 구하는 메서드를 만들어 보자
    • 두 점은 정수인 경우도 있고, 실수인 경우도 있으므로 제네릭 타입을 사용하여 구현한다.

    Point.java

    public class Point<T, V> {
    	
    	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;
        }
    }

    GenericMethod.java

    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<Integer, Double>(0, 0.0);
    		Point<Integer, Double> p2 = new Point<>(10, 10.0);
    		
    		double rect = GenericMethod.<Integer, Double>makeRectangle(p1, p2);
    		System.out.println("두 점으로 만들어진 사각형의 넓이는 " + rect + "입니다.");
    	}
    }

    댓글