我在做什么错?
问题描述:
该类是产品,方法是getName,getPrice和reducePrice: 我已经创建了构造函数和方法,但无法弄清楚为什么我的实例变量私有字符串名称出现错误,如何创建getName和getPrice身体为我的方法。我还没有创建我的ProductTester来实际打印结果,可以使用我可以获得的所有建议。我在做什么错?
public class Product
{
private double price;
private string name; // this has an error???
/**
Constructs a product with a given name and price.
@param name the name
@param price the price
*/
public Product(String n, double p)
{n = name;
p = price;}
/**
Gets the product name.
@return the name
*/
public String getName() // what is the body??
{ }
/**
Gets the product price.
@return the price
*/
public double getPrice() // what is the body???
{ }
/**
Reduces the product price.
@param amount the amount by which to reduce the price
*/
public void reducePrice(double amount)
{ price = price - amount;}
}
修订的类产品:
public class Product
{
private double price;
private String name;
/**
Constructs a product with a given name and price.
@param name the name
@param price the price
*/
public Product(String n, double p)
{n = name;
p = price;}
/**
Gets the product name.
@return the name
*/
public String getName()
{ return name;}
/**
Gets the product price.
@return the price
*/
public double getPrice()
{ return price; }
/**
Reduces the product price.
@param amount the amount by which to reduce the price
*/
public void reducePrice(double amount)
{ price = price - amount;}
public double price()
{ return price;}
}
这是我ProductPrinter类。制作两个产品,打印名称和价格,将价格降低5,然后重新打印:我在system.out.println(myProducts.reducePrice(5))和system.out.println(myProducts2.reducePrice(5) ))。
/** A class to test the Product class
*/
public class ProductPrinter
{
/** Tests the methods of the Product class.
* @param args not used
*/
public static void main(String[] args)
{
Product myProducts = new Product("TV", 499.00);
Product myProducts2 = new Product("Bed", 899.00);
System.out.println(myProducts.getName());
System.out.println(myProducts2.getName());
System.out.println(myProducts.getPrice());
System.out.println(myProducts2.getPrice());
System.out.println(myProducts.reducePrice(5));
System.out.println(myProducts2.reducePrice(5));
System.out.println(myProducts.price());
System.out.println(myProducts2.price());
}
}
答
您正在使用的string
代替String
所以只是把这个
private String name;
而且你是不是在你的方法getName()
和getPrice()
返回所需的值。
public String getName() {
return name;
}
public double getPrice() {
return price;
}
答
public Product(String n, double p)
{n = name;
p = price;}
他们是完全错误的方式:
public Product(String n, double p)
{name = n;
price = p;}
那么它应该是罚款。
错误:
private String name;
应该修正这个错误
方法体:你只需要在那里返回变量:
public String getName() // what is the body??
{
return name;
}
/**
Gets the product price.
@return the price
*/
public double getPrice() // what is the body???
{
return price;
}
答
首先,它是String
没有string
:
private String name; // Note the capital 'S'
在构造函数(public Product(){}
)中,你做错了!你可能要设置name = n
和price = p
,以便它可以在后面的程序中使用:
public Product(String n, double p){
name = n;
price = p;
}
而对于getName()
和getPrice()
方法,你只是想返回适当的值:
public String getName(){
return name;
}
public double getPrice(){
return price;
}
答
更换您的reducePrice方法使用下面的方法,因为您没有从此方法返回任何值,所以无法从System.out.println()调用void方法。
public double reducePrice(double amount)
{
return price = price - amount;
}
下面一个替换你的构造方法 -
public Product(String n, double p)
{
name = n; // n = name is not correct as you are assigning name value into your local variable n
price = p; // p = price , same case with price variable also
}
资本的“弦乐也许是吧.. – Revive 2014-09-30 20:43:05
注:使用'名称= N;',而不是'N =名称;' – Krayo 2014-09-30 20:45:58
在问这个问题之前,你应该真的了解这门语言的基础知识。 – manouti 2014-09-30 20:49:45