java - 如何在java中为两个类实现一个泛型方法

标签 java generics inheritance interface generic-method

我有一个接口(interface),它有一个普通方法和一个泛型方法。我已经为两个不同的类实现了普通方法,但现在不知道如何使用通用方法来实现。这是我的代码:

Sphere.java:

public class Sphere implements GeometricShape<Sphere> {

    private double radius;

    public Sphere (double radius) {
        this.radius = radius;
    }

    public double volume() {
        return (4.0 / 3.0) * Math.PI * radius * radius * radius;
    }

    public void describe() {
        System.out.println("Sphere[radius=" + radius + "]");
    }

    @Override
    public Sphere supersize()
    {
        this.radius*=2;
        return new Sphere(radius);
    }

}

矩形.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 supersize() 
    {

        this.width*=2;
        this.height*=2;
        return new Rectangle(width, height);
    }


}

TwoDShape.java:

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

}

ThreeDShape.java:

public interface ThreeDShape extends GeometricShape<ThreeDShape>
{
    public double volume();
}

GeometricShape.java:

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

}

最后是主类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());
     }



     public static void main(String[] args) {


        System.out.println("The 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();


        ArrayList<Sphere> spheres = new ArrayList<Sphere>();
        spheres.add(new Sphere(10.0));
        spheres.add(new Sphere(50.0));
        spheres.add(new Sphere(0.0));

        System.out.println("Example spheres");
        describe_all(spheres);
        System.out.println();
        System.out.println("The supersize() method:"); 
        System.out.println();

        ArrayList<Rectangle> double_rects = supersize_list(rects);
        describe_all(double_rects);
        System.out.println();

        ArrayList<Sphere> double_spheres = supersize_list(spheres);
        describe_all(double_spheres);

    }


}

如何实现 supersize_list 方法,它从矩形和球体中获取 supersize 方法并输出如下

Rectangle[width=4.0, height=6.0]
Rectangle[width=10.0, height=10.0]
Total number of shapes: 2

Sphere[radius=20.0]
Sphere[radius=100.0]
Sphere[radius=0.0]
Total number of shapes: 3

你能帮我解决这个问题吗?我非常感谢您的帮助!

最佳答案

类层次结构看起来不一致。例如,您有 ThreeDShape extends GeometricShape<ThreeDShape>TwoDShape extends GeometricShape同时,没有明显的原因。为这些类型编写泛型方法并不有趣。

这是一个不太令人困惑的版本。 (我希望)注意:我选择在 supersize 方法中不改变形状本身的大小,而是让它在保持原始形状不变的情况下返回更大的形状。

<强>1。几何形状

/**
 * A geometric shape interface. You can do two things with it.
 * 1. Ask it to describe itself (to stdout);
 * 2. Ask it to return a bigger version of itself (double the size).
 */
public interface GeometricShape<T extends GeometricShape<T>> {
    /**
     * Print a description to STDOUT
     */
    void describe();

    /**
     * Returns a bigger shape.
     * @return Something that's a GeometricShape
     */
    T supersize();
}

<强>2。 Shape2D 和矩形

/**
 * A 2-dimensional shape. 
 * It has area.
 * Its supersize() method should return a Shape2D instance.
 */
public interface Shape2D<T extends Shape2D<T>> extends GeometricShape<T> {

    double area();
}

/**
 * A rectangle.
 */
public final class Rectangle implements Shape2D<Rectangle> {

    private final double width;
    private final double height;

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

    @Override
    public String toString() {
        return "Rectangle{" +
                "width=" + width +
                ", height=" + height +
                '}';
    }

    @Override
    public void describe() {
        System.out.println(this);
    }

    @Override
    public Rectangle supersize() {
        return new Rectangle(width*2, height*2);
    }

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

<强>3。 Shape3D 和球体

/**
 * A 3-dimensional shape. 
 * It has volume. 
 * Its supersize() method should return a Shape3D instance.
 */
public interface Shape3D<T extends Shape3D<T>> extends GeometricShape<T> {

    double volume();
}

/**
 * A sphere
 */
public final class Sphere implements Shape3D<Sphere> {
    private final double radius;

    public Sphere(double radius) {
        this.radius = radius;
    }

    @Override
    public String toString() {
        return "Sphere{" +
                "radius=" + radius +
                '}';
    }

    @Override
    public void describe() {
        System.out.println(this);
    }

    @Override
    public Sphere supersize() {
        return new Sphere(radius*2);
    }

    @Override
    public double volume() {
        return 4*Math.PI*Math.pow(radius, 3)/3;
    }
}

现在是转换列表的通用方法

public static <T extends GeometricShape<T>>
List<T> supersize_list(List<T> list) {
    List<T> result = new ArrayList<>();
    for (T shape : list) {
        result.add(shape.supersize());
    }
    return result;
}

关于java - 如何在java中为两个类实现一个泛型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40776350/

相关文章:

java - 将 JLabel 放置在面板的中央,将 JLabel 放置在同一面板的右侧

ios - 如何在 iOS 中自己创建的模型类中使用泛型?

c# - 如何访问继承Collection<>的类中的集合?

java - 从 JUnit 测试调用静态方法而不指定类

java - Spring bean 未使用 @Repository 注解标识

java - 在泛型类中使用泛型对象方法

c# - 如何使用弱类型参数调用泛型方法

java - 我可以使用父类(super class)的类工厂静态方法创建子类实例吗?

python - 在 Python 3 中扩展一个类并使用 __init__ 构造它

java - JAXB:按特定顺序排列的多个同类标签