【问题标题】:Implementing generic method from interface in java从java中的接口实现泛型方法
【发布时间】:2016-11-23 01:09:14
【问题描述】:

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

矩形.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 方法相同,即: 矩形[宽度=2.0,高度=3.0] 矩形[宽度=5.0,高度=5.0] 如果可以的话,请告诉我哪里出错了?

【问题讨论】:

    标签: java generics interface generic-method


    【解决方案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 获取的当前对象的宽度和高度值。 您可能必须更改 increase_twice() 的定义,因为我认为您不需要返回一个新的 Rectangle 对象。

    让我知道这是否有效。

    【讨论】:

    • 非常感谢@Yan,是的,我明白了
    • 太好了。编码快乐!
    【解决方案2】:

    Rectangle.increase_twice() 返回一个新对象。但是,在 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多