我正在尝试按照 Programming in Scala 一书中的第 6.2 章构建 Rational。 但是我在尝试这样做时遇到了问题。
这是test.scala:
class Rational(n: Int, d: Int)
{
println("Created "+n+"/"+d)
}
所以我首先在我的终端窗口中输入以下内容:
user$ scala
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_73).
Type in expressions to have them evaluated.
Type :help for more information.
然后使用:load test.scala
scala> :load test.scala
Loading test.scala...
defined class Rational
<console>:12: error: not found: value n
println("Created "+n+"/"+d)
^
<console>:12: error: not found: value d
println("Created "+n+"/"+d)
^
当我输入 new Rational(1, 2)
时,我很期待。
Created 1/2
res0: Rational = Rational@90110a
但是结果是
res0: Rational = Rational@9e89d68
解释器只返回第二行。如何打印出此调试消息?
顺便说一句,我使用的是 Mac 操作系统。
如有任何帮助,我们将不胜感激。提前谢谢你。
已更新
这是正确的做法。
class Rational(n: Int, d: Int) {
println("Created "+n+"/"+d)
}
最佳答案
分号推理会毁了你的一天。
Scala 编译器解释
class Rational(n: Int, d: Int)
{ println("Created "+n+"/"+d)
}
作为
class Rational(n: Int, d: Int);
{ println("Created "+n+"/"+d);
}
糟糕!
试试这个:
class Rational(n: Int, d: Int) {
println("Created "+n+"/"+d)
}
编译器不再在第一行末尾推断分号,因为左大括号表示还有更多内容。
它应该工作得更好。
关于scala - 为什么我不能在 Scala 中打印调试消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826787/