2016-11-24 51 views
0

我有一個有一個普通方法和一個通用方法的接口。我已經爲兩個不同的類實現了普通的方法,但是現在不用如何用泛型方法來實現。這裏是我的代碼:如何在java中爲兩個類實現一個通用方法

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

} 

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

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方法,它需要超大方法從兩個矩形和領域和輸出,如

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 

你能幫我解決這個問題嗎?我非常感謝你的幫助!

+0

我很困惑,爲什麼'supersize'將返回一個新對象的泛型方法,它應該不只是增加現有對象的大小? –

+1

@ScaryWombat我同意,它應該做,但不是兩者。 –

+0

是的,我想在球體和矩形兩次增加大小。你有什麼建議?我想寫一個supersize_list方法,返回尺寸增加的矩形和球體@Scary Wombat @ Luke Lee –

回答

1

的類層次看起來不協調。例如,您同時擁有ThreeDShape extends GeometricShape<ThreeDShape>TwoDShape extends GeometricShape,原因不明。爲這些類型編寫泛型方法並不有趣。

這是一個不太令人困惑的版本。 (我希望)注意:我選擇不在supersize方法中更改形狀本身的大小,而是讓它在保持原始大小不變的情況下返回更大的形狀。

1. GeometricShape

/** 
* 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; 
} 
+0

謝謝@Luke Lee的好解釋!但是,它在主體中的supersize_list方法中都顯示錯誤,因此它仍然與supersize_list不匹配的球體和矩形,並且需要在它們前面進行強制轉換才能正確運行。我不明白爲什麼會出現這個錯誤 –

+0

你是否將結果投射到'ArrayList'?嘗試將它們聲明爲'List'。 '列表 double_rect = ...'。 –

+0

不,我沒有投出結果,爲了正確運行,我鑄造了ArrayList double_rects和ArrayList double_spheres。我的意思是不改變這兩個,我怎麼能在泛型方法中做到這一點? –

0

你不需要返回一個新的對象。對於Rectangle例如

@Override 
public void supersize() 
{ 

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

足夠

+0

然後它不會是通用的。但對我來說很好。 –

+0

在這種情況下,我應該將supersize更改爲GeometricShape中的普通方法。我想在不改變它的情況下實現T supersize() –

+0

@LukeLee由於'supersize'作用於具體對象的字段,因此不涉及泛型。 **超大的幾何**也應該改變 –

相關問題