简体   繁体   English

从Java接口实现通用方法

[英]Implementing generic method from interface in java

I have two interfaces and one class implementing them. 我有两个接口和一个实现它们的类。 In the interface I have shown one usual method and one generic method. 在界面中,我展示了一种常用方法和一种通用方法。 When implementing them in main method usual method shows correct result, but the generic one does not. 在main方法中实现它们时,通常的方法会显示正确的结果,而通用方法则不会。 If possible could you tell me, why the increase_twice() method does not return correct result, please? 如果可以的话,请您告诉我,为什么gain_twice()方法没有返回正确的结果? Here are my code: 这是我的代码:

Rectangle.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: TwoDShape.java:

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


}

GeometricShape.java: GeometricShape.java:

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

}

And finally test class ArrayListExample.java 最后测试类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);

    }


}

I expect that the increase_twice will return rectangles with Rectangle[width=4.0, height=6.0] Rectangle[width=10.0, height=10.0] but it instead return the same as describe method which is: Rectangle[width=2.0, height=3.0] Rectangle[width=5.0, height=5.0] If possible could you tell me where I make a mistake please? 我期望crement_twice将返回矩形为Rectangle [width = 4.0,height = 6.0] Rectangle [width = 10.0,height = 10.0]的矩形,但返回的描述方法相同:Rectangle [width = 2.0,height = 3.0 ] Rectangle [width = 5.0,height = 5.0]如果可以,请告诉我哪里出错了?

I think the issue is in increase_twice function. 我认为问题出在增量函数中。 When you are calling rects.get(i).increase_twice(); 当您调用rects.get(i).increase_twice(); you are actually creating a new Rectangle object with doubled values and returning. 您实际上是在创建一个新的Rectangle对象,该对象的值加倍并返回。 You are not updating the current object. 您没有更新当前对象。 The increase_twice method should be: gain_twice方法应为:

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

This way you will update the width and height values of the current object that you are getting from the ArrayList. 这样,您将更新从ArrayList获取的当前对象的宽度和高度值。 You might have to change the definition of increase_twice() because i don't think you need to return a new Rectangle object. 您可能必须更改creation_twice()的定义,因为我认为您不需要返回新的Rectangle对象。

Let me know if that works. 让我知道是否可行。

Rectangle.increase_twice() returns a new Object. Rectangle.increase_twice()返回一个新的Object。 However, in ArrayListExample.increase_size(), the returned object isn't set. 但是,在ArrayListExample.increase_size()中,未设置返回的对象。 You may modify that method to this: 您可以对此修改方法:

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

Or you may modify Rectangle.increase_twice() to this: 或者,您可以将Rectangle.increase_twice()修改为此:

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

And GeometricShape.increase_twice() declaration to this: 和GeometricShape.increase_twice()声明为:

public void increase_twice();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM