Java - Class无法在其他类中看到对象

时间:2015-05-01 16:06:18

标签: java class

我遇到了一个常见的编译错误,无法在java中找到符号。 Product类中的方法不能在主类中使用。代码如下。我试图以任何可能的方式通过课程(据我所知),但没有运气。

我得到的错误是:

CashRegisterTester.java:108: error: cannot find symbol
        double iprice = products[i].getPrice();
                        ^
  symbol:   variable products
  location: class CashRegister
1 error

这是代码。

import java.util.Scanner;

class CashRegisterTester {
    public static void main(String[] args) {

        Product[] products = new Product[5];

        products[0] = new Product();
        products[0].setCode(0);
        products[0].setName("psomi");
        products[0].setPrice(0.50);

        products[1] = new Product();
        products[1].setCode(1);
        products[1].setName("gala");
        products[1].setPrice(1.30);

        products[2] = new Product();
        products[2].setCode(2);
        products[2].setName("mila");
        products[2].setPrice(1.80);

        products[3] = new Product();
        products[3].setCode(3);
        products[3].setName("zaxari");
        products[3].setPrice(2.40);

        products[4] = new Product();
        products[4].setCode(4);
        products[4].setName("krasi");
        products[4].setPrice(13.20);


        for (int i = 0; i < 5; i++)
            System.out.println("\n Product code: " + products[i].getCode() + " \n Product Name: " + products[i].getName() + "\n Product Price: " + 
                    products[i].getPrice());

        System.out.println("Which product would you like to buy?");
        Scanner myscanner = new Scanner(System.in);
        int iEntered;
        iEntered = myscanner.nextInt();

        CashRegister register = new CashRegister(products);

        register.recordPurchase(iEntered);

        System.out.println("Would you like to buy something more? [y/n]");
        String answer;
        answer = myscanner.next();


        while (answer.equals('y')) {
            System.out.println("Which product would you like to buy?");

            register.recordPurchase(iEntered);

            System.out.println("Would you like to buy something more? [y/n]");
            answer = myscanner.next();

        }

        System.out.println("How much money will you give?");

        double pay = myscanner.nextDouble();
        register.enterPayment(pay);

        double change = register.giveChange();
        System.out.println(change);

        // register.finalReceipt();


    }

}
class Product{

    //Product fields
    public int code;
    public String name;
    public double price;


    //Getters & setters
    public int getCode(){
        return code;
    }

    public void setCode(int scode){
        code = scode;
    }

    public String getName(){
        return name;
    }

    public void setName(String sname){
        name = sname;
    }

    public double getPrice(){
        return price;
    }

    public void setPrice(double sprice){
        price = sprice;
    }
}
public class CashRegister extends Product{


    public CashRegister(Product[] products){
        purchase = 0;
        payment = 0;
    }

    public void recordPurchase(int i){
        double iprice = products[i].getPrice;
        double total = purchase + iprice;
        purchase = total;
    }

    public void enterPayment(double amount){
        payment = amount;
    }

    public double giveChange(){
        double change = payment - purchase;
        purchase = 0;
        payment = 0;
        return change;
    }

    private double purchase;
    private double payment;

}

1 个答案:

答案 0 :(得分:1)

products变量无处声明。这就是你得到这个例外的原因。

products声明为CashRegister类的私有成员,并在CashRegister构造函数中初始化它。请参阅以下代码:

public class CashRegister extends Product {

    public CashRegister(Product[] products){
        purchase = 0;
        payment = 0;
        this.products=products; // Initialize private member
    }

    public void recordPurchase(int i){
        double iprice = products[i].getPrice();
        double total = purchase + iprice;
        purchase = total;
    }

    public void enterPayment(double amount){
        payment = amount;
    }

    public double giveChange(){
        double change = payment - purchase;
        purchase = 0;
        payment = 0;
        return change;
    }

    private double purchase;
    private double payment;
    private Product[] products; // Declare private member
}

最后一点,为了防止资源泄漏,千万不要忘记关闭Scanner。 因此,在myscanner.close();类的main方法的末尾添加CashRegister