【问题标题】:why does this code keeps on showing error为什么这段代码一直显示错误
【发布时间】:2020-08-01 17:53:54
【问题描述】:
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

【问题讨论】:

    标签: java methods constructor instance-variables


    【解决方案1】:

    类中的字段名为productType,您在构造函数签名中通过其名称引用它。所以使用:

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

    【讨论】:

      猜你喜欢
      • 2017-11-25
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      相关资源
      最近更新 更多