所以我正在做这个模拟汽车仪表的练习。一共有三个类:FuelGauge
、Odometer
和 CarInstrumentSimulator
(具有 main 方法的类)。前两个都有我定义的构造函数。但是,每当我在 main
中输入以下内容时:
public static void main(String[] args) {
CarInstrumentSimulator carInstrumentSimulator = new CarInstrumentSimulator();
FuelGauge fuel = carInstrumentSimulator.new FuelGauge();
Odometer odometer = carInstrumentSimulator.new Odometer(0, fuel);
我总是得到一个 CarInstrumentSimulator.FuelGauge 无法解析为 eclipse 中的类型错误(对于里程表也是如此),但是我从我获得练习的网站(https://www.leveluplunch.com/java/exercises/car-instrument-simulator/)中详细的更正中得到了这行代码。 ) 我对 java 和编码非常陌生,所以我想知道: 1)这个语法是什么意思:
FuelGauge fuel = carInstrumentSimulator.new FuelGauge();
2)为什么这个语法有问题?
提前致谢^^
最佳答案
我认为 source 中存在拼写错误。 。试试这个:
FuelGauge fuel = new FuelGauge();
Odometer odometer = new Odometer(0, fuel);
这个问题没有合理的答案......
what does this syntax mean :
FuelGauge fuel = carInstrumentSimulator.new FuelGauge();
...因为有问题的行是乱码;)
Java 对象(例如 carInstrumentSimulator
)上的有效方法调用需要点符号、方法名称以及左括号和右括号,例如carInstrumentSimulator.doSomething()
但上面的代码没有左括号和右括号,使用了单词 new
这不是一个有效的 Java 方法名称(因为它是一个保留的方法名称)关键字)和 new
后面是 FuelGauge()
这使得整个事情无法解释。
有关 Java 方法构造的更多详细信息 here .
如果 FuelGauge
在 CarInstrumentSimulator
中声明,则此语法将有效:
new CarInstrumentSimulator.FuelGauge();
类似地,如果 CarInstrumentSimulator
公开了 FuelGauge
的创建者方法,则以下语法将有效:
carInstrumentSimulator.newFuelGauge();
但此语法在任何情况下都无效:carInstrumentSimulator.new FuelGauge();
。
关于java - Eclipse错误: cannot be resolved to a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45300857/