0

我的代码工作正常并且正在执行,我的问题是 IntelliJ 在类的方法上给了我一个“无法解析符号”错误。

代码如下:

// Q1. Create a Sailboat class with methods to raise and lower the sails,
// printing Sails raised, and Sails lowered, respectively.
// Create a Motorboat class with methods to start and stop the motor,
// returning Motor on, and Motor off, respectively. Create an object (instance)
// of the Sailboat class. Use assert for verification:

class Sailboat {
  def raise  = "Sails raised"
  def lower = "Sails lowered"
}

val sailboat = new Sailboat
val r1 = sailboat.raise
assert(r1.equals("Sails raised"), "Expected Sails raised, Got " + r1)
val r2 = sailboat.lower
assert(r2.equals("Sails lowered"), "Expected Sails lowered, Got " + r2)

请参阅附加的 inteliJ 错误的屏幕截图,这发生在方法和 equals 方法上。 InteliJ 错误

我有一个非常相似的脚本,基本上是一样的,但它没有给出任何错误:

class Motorboat {
  def on  = "Motor on"
  def off = "Motor off"
}

val motorboat = new Motorboat
val s1 = motorboat.on
assert(s1 == "Motor on", "Expected Motor on, Got " + s1)
val s2 = motorboat.off
assert(s2 == "Motor off", "Expected Motor off, Got " + s2)

请参阅附加的类似代码的屏幕截图,没有错误: IntelliJ 中没有错误的类似代码

我尝试过的;

  1. 重写类和方法
  2. 重新启动 IDE
  3. 重新启动我的笔记本电脑
  4. 更新 IntelliJ
  5. 谷歌搜索为什么会这样

任何关于如何解决这个问题的想法将不胜感激

谢谢!

4

1 回答 1

0

非常奇怪的行为,但我设法通过在同一个文件中添加第三类来解决这个问题;

class Flare {
  def light = "Flare used!"
}

val flare = new Flare
val f1 = flare.light
assert(f1 == "Flare used!", "Expected Flare used!, Got " + f1)

这已经消除了帆船类的错误,并且代码在 IntelliJ 上很清晰

很奇怪....

于 2016-01-26T18:12:40.997 回答