0

1 つの通常のメソッドと 1 つのジェネリック メソッドを持つインターフェイスがあります。2 つの異なるクラスに通常のメソッドを実装しましたが、ジェネリック メソッドでそれを行う方法はまだわかりません。これが私のコードです:

スフィア.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);
    }

}

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

幾何学的形状.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);

    }


}

長方形と球の両方からスーパーサイズメソッドを取得し、次のような出力を行うスーパーサイズリストメソッドを実装するにはどうすればよいですか

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

これで私を助けてくれませんか?あなたの助けに感謝します!

4

2 に答える 2

1

クラス階層に一貫性がないように見えます。たとえば、明らかな理由もなく、同時に持っThreeDShape extends GeometricShape<ThreeDShape>ています。TwoDShape extends GeometricShapeこれらの型のジェネリック メソッドを作成するのは楽しくありません。

これは、混乱の少ないバージョンです。(願っています)注:スーパーサイズメソッドでは形状自体のサイズを変更しないことを選択しました。代わりに、元の形状を変更せずに、より大きな形状を返すようにしました。

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 と Rectangle

/**
 * 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;
}
于 2016-11-24T01:46:35.387 に答える
0

新しいオブジェクトを返す必要はありません。たとえばRectangle_

@Override
public void supersize() 
{

    this.width*=2;
    this.height*=2;
}

十分なものです

于 2016-11-24T01:22:03.443 に答える