2016-11-23 86 views
0

我有两个接口和一个类来实现它们。在界面中,我展示了一种常用的方法和一种通用的方法。在主要方法中执行它们时,常规方法显示正确结果,但通用方法不会。如果可能的话,你能告诉我,为什么increase_twice()方法没有返回正确的结果?这里是我的代码:在java中实现通用接口方法

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); 

    } 


} 

我预计增加矩形会返回矩形[宽度= 4.0,高度= 6.0] 矩形[宽度= 10.0,高度= 10.0],但它返回相同的描述方法是: Rectangle [width = 2.0,height = 3.0] Rectangle [width = 5.0,height = 5.0] 如果可能,你能告诉我我在哪里犯了一个错误吗?

回答

1

我认为问题是在增加的函数。当你打电话给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中获取当前对象的宽度和高度值。 您可能必须更改increase_twice()的定义,因为我不认为您需要返回新的Rectangle对象。

让我知道这是否有效。

+1

非常感谢你@Yan,是的,我的理解是 –

+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()这样的:

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

而且GeometricShape.increase_twice()声明如下:

public void increase_twice();