我究竟做错了什么?

时间:2014-09-30 20:42:14

标签: java eclipse methods

该类是Product,方法是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());

    }

}

4 个答案:

答案 0 :(得分:0)

您使用string代替String

所以只需把这个

private String name;

并且您没有在方法getName()getPrice()中返回所需的值。

public String getName() {
    return name;
}

public double getPrice() {
    return price;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

首先是String而非string

private String name; // Note the capital 'S'

在构造函数(public Product(){})中,你做错了!您可能希望设置name = nprice = p,以便稍后在程序中使用它:

public Product(String n, double p){
    name = n;
    price = p;
}

对于getName()getPrice()方法,您只想返回适当的值:

public String getName(){
    return name;
}
public double getPrice(){
    return price;
}

答案 3 :(得分:0)

使用以下方法替换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
}