2014-05-03 41 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

相关问题