我不明白我的编译器如何找不到类 Weight
,考虑到我不是已经在我的 public boolean checkWeight
方法中创建了它吗?编译器指向这一行:
`Weight first = new Weight();`
和
`Weight second = new Weight();`
<小时/>
public class Guard {
int stashWeight;
public Guard ( int maxWeight ) {
this.stashWeight = maxWeight;
}
public boolean checkWeight (Weight maxWeight) {
if ( maxWeight.weight >= stashWeight ) {
return true;
} else {
return false;
}
}
public static void main (String[] args ) {
Weight first = new Weight();
first.weight = 3000;
Weight second = new Weight();
second.weight = 120;
Guard grams = new Guard(450);
System.out.println("Officer, can I come in?");
boolean canFirstManGo = grams.checkWeight(first);
System.out.println(canFirstManGo);
System.out.println();
System.out.println("Officer, how about me?");
boolean canSecondManGo = grams.checkWeight(second);
System.out.println(canSecondManGo);
}
}
最佳答案
您应该像这样定义Weight
类:
public class Weight {
public int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
输出:
Officer, can I come in?
true
Officer, how about me?
false
或者使用 import
关键字导入您的 Weight
类。
关于java - 创建对象时如何修复 "Error: Cannot find symbol"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370592/