2014-05-03 42 views
0

嗨,我是一個開始的程序員,我想我有一個簡單的問題,但我找不到答案。我希望你能幫助我:D我總共有四節課,但我需要解決的是在課堂上。我有一個簡單的方法,需要類Product的Price參數。但它不起作用,因爲我無法訪問此參數。從另一個類獲取受保護的屬性

這是Person類用來購買產品的方法。一個人的預算必須大於或等於價格,如果不是>他不能購買該產品。但是如果他還沒有擁有該產品,他只能購買該產品。

public class Person{ 

     private String name; 
     private double budget; 
    private ArrayList<Product> allPossessions; 
    private Product theProduct; //association with class product, I have all getters and setters etc. 

    // the method::: 
    public boolean Buy(Product p) { 

    if (hasProduct(null) == true) { 

    if (budget >= Product.getThePrice()) {  
    // getTheprice does not work, how do i get this working? How do i get this parameter from class product? 

    return true; 

    } 

return false; 

} 

return false; 
    } 

這是類產品,我需要有價格,創建方法。

public abstract class Product { 

    protected String Design; 

    protected int yearOfPurchase; 

    protected double thePrice; 
} 

謝謝你的時間,原諒我英文破碎!謝謝:)

+2

那麼你可以不添加'getThePrice()'方法到'Product'?據我所知,目前沒有這樣的方法...... –

回答

0

你應該爲產品類中的價格參數編寫一個getter方法。

public double getPrice() { 
    return thePrice; 
} 
+0

WOW哈哈天才非常感謝你。我從來沒有想過是那麼簡單的xD – user3599415

相關問題