gpt4 book ai didi

java - 为什么这段代码一直显示错误

转载 作者:行者123 更新时间:2023-12-02 08:42:23 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - 为什么这段代码一直显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61301966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com