[英]Scala trait example not working as it should
I am trying to run tutorialspoint example 我正在尝试运行tutorialspoint示例
trait Equal {
def isEqual(x: Any): Boolean
def isNotEqual(x: Any): Boolean = !isEqual(x)
}
class Point(xc: Int, yc: Int) extends Equal {
var x: Int = xc
var y: Int = yc
def isEqual(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == y
}
object Demo {
def main(args: Array[String]) {
val p1 = new Point(2, 3)
val p2 = new Point(2, 4)
val p3 = new Point(3, 3)
println(p1.isNotEqual(p2))
println(p1.isNotEqual(p3))
println(p2.isNotEqual(2))
}
}
I got no print on screen if I run from terminal 如果从终端运行,屏幕上没有任何打印
milenko@milenko-desktop:~$ scala d8.scala
If I run code from eclipse,then I have 如果我从eclipse运行代码,那么我有
Why do I have problem with this simple example? 为什么这个简单的示例有问题?
I think you might be missing a few steps in the edit-compile-run loop, because it works fine for me. 我认为您可能在edit-compile-run循环中缺少一些步骤,因为它对我来说很好。
prompt> vi demo.scala # input your code here
prompt> scalac demo.scala # compile the code, creates .class files
prompt> scala Demo # object Demo has a "main" method
true
false
true
prompt>
You need to compile first 你需要先编译
scalac d8.scala
then 然后
scala Demo
The scala command wont run source files if they have traits scala命令不会运行具有特征的源文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.