在java中从接口实现泛型方法

在java中从接口实现泛型方法,java,generics,interface,generic-method,Java,Generics,Interface,Generic Method,我有两个接口和一个实现它们的类。在界面中,我展示了一个常用方法和一个通用方法。当在主方法中实现它们时,通常的方法显示正确的结果,但泛型方法不显示正确的结果。如果可能的话,你能告诉我为什么increase_tweep()方法不能返回正确的结果吗?这是我的代码: Rectangle.java: public class Rectangle implements TwoDShape { private double width, height; public Rectangle (

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

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


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

}
GeometricShape.java:

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


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

    }


}
import java.util.ArrayList;
公共类ArrayListExample{

public static void descripe_all(ArrayList我认为问题出在increase_tweep函数中。当您调用
rects.get(I).increase_tweep();
时,实际上是在创建一个新的矩形对象,该对象的值加倍并返回。您没有更新当前对象。 增加两倍的方法应为:

@Override
    public void increase_twice() {
        // TODO Auto-generated method stub
        this.width *= 2;
        this.height *= 2;
    }
这样,您将更新从ArrayList获取的当前对象的宽度和高度值。 您可能需要更改crease_tweep()的定义,因为我认为您不需要返回新的矩形对象


让我知道这是否有效。

我认为问题出在increase\u tweep函数中。当您调用
rects.get(I).increase\u tweep();
时,实际上是在创建一个新的矩形对象,该对象的值加倍并返回。您没有更新当前对象。 增加两倍的方法应为:

@Override
    public void increase_twice() {
        // TODO Auto-generated method stub
        this.width *= 2;
        this.height *= 2;
    }
这样,您将更新从ArrayList获取的当前对象的宽度和高度值。 您可能需要更改crease_tweep()的定义,因为我认为您不需要返回新的矩形对象

让我知道这是否有效。

Rectangle.increase\u tweep()返回一个新对象。但是,在ArrayListExample.increase\u 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;
}
和GeometricShape.increase_two()声明:

public void increase_twice();
Rectangle.rease_tweep()返回一个新对象。但是,在ArrayListExample.rease_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;
}
和GeometricShape.increase_two()声明:

public void increase_twice();

非常感谢@Yan,是的,这很有效,我理解这很好。快乐编码!非常感谢@Yan,是的,我理解这很好。快乐编码!