为什么此代码不断显示错误

时间:2020-04-19 09:14:01

标签: java methods constructor instance-variables

public class Store {

    //instance fields 
    String productType;
    int inventoryCount;
    double inventoryPrice; 

    //constructor method
    public Store(String product,int count,double price) {
        productType = product;
        inventoryCount = count;
        inventoryPrice = price;
    }

    //main method 
    public static void main(String[] args) {
        Store cookieShop = new Store("cookies",12,3.75);
        System.out.println("my cookie shop menu  " + cookieShop.product);
    }
}

为什么会不断显示此错误?

Store.java:16: error: cannot find symbol
    System.out.println("my cookie shop menu  " + cookieShop.product);
                                                           ^
  symbol:   variable product
  location: variable cookieShop of type Store
1 error

1 个答案:

答案 0 :(得分:1)

该类中的字段名为productType,您在构造函数签名中通过其名称进行引用。因此使用:

    Store cookieShop = new Store("cookies", 12, 3.75);
    System.out.println("my cookie shop menu  " + cookieShop.productType);