2015-10-02 88 views
3

寫這樣的類是否正確?有問題的是Item類中的方法getPrice()。每個項目需要有一個getPrice()。但我實際上不能回報什麼。所以我發射this.getPrice()讓我的價格ProductItem。是否有更堅實/更好設計的解決方案?返回類型的Java Inherance方法

class Item { 
    String description; 

    public Item(String description) { 
     this.description = description; 
    } 

    double getPrice(){return this.getPrice();} //TODO Correct like this? 
} 

class ProductItem extends Item { 
    int amount; 
    double pricePerUnit; 

    public ProductItem(String description, int amount, double pricePerUnit)   { 
     super(description); 
     this.amount = amount; 
     this.pricePerUnit = pricePerUnit; 
    } 

    @Override 
    double getPrice(){ 
     return amount * pricePerUnit; 
    } 
} 
+0

'this.getPrice()'在'Item'中不存在。你只需要做'返回0'。覆蓋類返回正確的東西,因爲它是「覆蓋」 – kevintjuh93

回答

13

這聽起來像Item應該是一個抽象類,然後,用getPrice()是一個抽象方法:

public abstract class Item { 
    private final String description; 

    public Item(String description) { 
     this.description = description; 
    } 

    public abstract double getPrice(); 

    public String getDescription() { 
     return description; 
    } 
} 

這意味着你將不能寫

Item item = new Item("foo"); // Invalid, because Item is abstract 

但你可以這樣寫:

Item item = new ProductItem("foo", 10, 2.0); 
double p = item.getPrice(); // 20.0 

您聲明的每個具體(非抽象)子類將不得不覆蓋getPrice()並提供一個實現。

查看abstract classes and methods section of the Java tutorial瞭解更多詳情。

0

只是在您錯過了它的情況下,類存在問題,其中方法getPrice()是遞歸的。所以這裏new Item().getPrice()將導致StackOverflowException。

您可以將課程設置爲抽象類。

abstract class Item { 
    String description; 
    public Item(String description) { 
    this.description = description; 
    } 
    abstract double getPrice(); // force child classes to override this method 
} 
4

你所要做的就是讓你的Itemabstract類,通過使getPrice方法抽象。

這會強制你的子類實現特定的功能,如果他們不編譯器會給出錯誤。

你可以做到這一點,如下所示:

class Item { 
    String description; 

    public Item(String description) { 
     this.description = description; 
    } 

    abstract double getPrice(); 
} 

你實際上給因執行圓形(無限循環)時,子類忘了實現該功能調用本身。當子類實現該函數時,它根本就不會被調用,因爲它被子類覆蓋。

1

你爲什麼不能讓getPrice()Itemabstract,如下所示:

public abstract class Item { 
    private String description; 

    public Item(final String description) { 
     this.description = description; 
    } 

    protected abstract double getPrice(); 

    public String getDescription() { 
     return description; 
    } 
} 

,並提供該類延伸的同一執行如下命令:

public class ProductItem extends Item { 
    private int amount; 
    private double pricePerUnit; 

    public ProductItem(final String description, final int amount, final double pricePerUnit) { 
     super(description); 
     this.amount = amount; 
     this.pricePerUnit = pricePerUnit; 
    } 

    @Override 
    protected double getPrice() { 
     return amount * pricePerUnit; 
    } 

} 
0

double getPrice(){return this.getPrice();}基本上是無限循環。 您應該先設定價格。

class Item { 
    String description; 
    double price; 
    public Item(String description) { 
     this.description = description; 
    } 

    public void setPrice(double price) { 
     this.price = price; 
    } 

    double getPrice(){return this.price;} 
}