有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么这段代码总是显示错误

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) 个答案

  1. # 1 楼答案

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

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