2016-11-23 6 views
0

두 개의 인터페이스와 하나의 클래스를 구현했습니다. 인터페이스에서 나는 하나의 일반적인 방법과 하나의 일반적인 방법을 보여 주었다. main 메소드에서 메소드를 구현할 때 일반적인 메소드는 올바른 결과를 보여 주지만 일반적인 메소드에서는 그렇지 않습니다. 가능한 경우 increase_twice() 메서드가 올바른 결과를 반환하지 않는 이유를 말해 줄 수 있습니까?자바에서 인터페이스의 제네릭 메소드 구현

Rectangle.java :

public class Rectangle implements TwoDShape { 

    private double width, height; 

    public Rectangle (double width, double height) { 
     this.width = width; 
     this.height = height; 
    } 

    public double area() { 
     return width * height; 
    } 

    public double perimeter() { 
     return 2.0 * (width + height); 
    } 

    public void describe() { 
     System.out.println("Rectangle[width=" + width + ", height=" + height + "]"); 
    } 

    @Override 
    public Rectangle increase_twice() { 
     // TODO Auto-generated method stub 
     return new Rectangle(2*this.width, 2*this.height); 
    } 


} 

TwoDShape.java :

public interface TwoDShape extends GeometricShape { 
    public double area(); 


} 

GeometricShape.java :

public interface GeometricShape<T extends GeometricShape<T>> 
{ 
    public void describe(); 
    public T increase_twice(); 

} 

그리고 마지막으로 테스트 클래스의 ArrayListExample.java

여기 내 코드입니다
import java.util.ArrayList; 


public class ArrayListExample { 



    public static void describe_all(ArrayList<? extends GeometricShape> shapes) 

    { 
     for(int i=0;i<shapes.size();i++) 
     { 
      shapes.get(i).describe(); 

     } 
     System.out.println("Total number of shapes:"+ shapes.size()); 
    } 


    private static ArrayList<Rectangle> increase_size(ArrayList<Rectangle> rects) 
    { 
      for(int i=0;i<rects.size();i++) 
      { 
      rects.get(i).increase_twice(); 

      } 
     return rects; 

    } 


    public static void main(String[] args) { 


     System.out.println("The result of describe() method:"); 

     System.out.println(); 
     System.out.println("Example rectangles"); 
     ArrayList<Rectangle> rects = new ArrayList<Rectangle>(); 
     rects.add(new Rectangle(2.0, 3.0)); 
     rects.add(new Rectangle(5.0, 5.0)); 
     describe_all(rects); 
     System.out.println(); 

     System.out.println("The result of increase_twice() method:"); 
     System.out.println(); 
     System.out.println("Example rectangles after increase:"); 
     ArrayList<Rectangle> double_rects = increase_size(rects); 
     describe_all(double_rects); 

    } 


} 

나는 increase_twice가 Rectangle [width = 4.0, height = 6.0] Rectangle [width = 10.0, height = 10.0]을 사용하여 사각형을 반환하지만 대신에 describe 메서드와 동일하게 반환 할 것이라고 예상한다. Rectangle [width = 2.0, height = 3.0] 직사각형 [width = 5.0, height = 5.0] 가능하면 실수 한 부분을 알려주시겠습니까?

답변

1

문제는 increase_twice 함수에 있다고 생각합니다. rects.get(i).increase_twice();을 호출 할 때 실제로 값이 두 배로되고 반환되는 새 Rectangle 객체를 만듭니다. 현재 오브젝트를 갱신하지 않습니다. increase_twice 방법은 다음과 같아야합니다

@Override 
    public void increase_twice() { 
     // TODO Auto-generated method stub 
     this.width *= 2; 
     this.height *= 2; 
    } 

이 방법 당신은 당신이 ArrayList를에서 얻고있는 현재 객체의 폭과 높이 값을 업데이트합니다. 새로운 Rectangle 객체를 반환 할 필요가 없다고 생각하기 때문에 increase_twice()의 정의를 변경해야 할 수도 있습니다.

작동하는지 알려주세요.

+1

대단히 감사합니다. @ 예, 그렇습니다. 이해했습니다. –

+0

좋습니다. 해피 코딩! – Yan

1

Rectangle.increase_twice()는 새 Object를 반환합니다. 그러나 ArrayListExample.increase_size()에서 반환 된 객체는 설정되어 있지 않습니다.

private static ArrayList<Rectangle> increase_size(ArrayList<Rectangle> rects) { 
    for (int i = 0; i < rects.size(); i++) { 
     rects.set(i, rects.get(i).increase_twice()); 
    } 
    return rects; 
} 

또는 당신은이에 Rectangle.increase_twice()을 수정할 수 있습니다 : 이 해당 방법을 수정할 수 있습니다

public void increase_twice(); 
이에

@Override 
    public void increase_twice() { 
     // TODO Auto-generated method stub 
     this.width = 2 * this.width; 
     this.height = 2 * this.height; 
    } 

그리고 GeometricShape.increase_twice() 선언