scala class 中的 println 不在 STDOUT 上显示输出

println within a scala class does not show output on STDOUT

我在 tutorialspoint 上找到了这段代码。代码看起来非常简单。它从 class 点调用 move 方法。该方法在 STDOUT 上打印新的 x/y 坐标。但是,它没有,我无法弄清楚为什么。我 运行 cygwin 上的 scala 和 cloudera VM 上的 spark-shell 上的代码。同样的结果:-)

如果有人能指出(没有双关语意)我做错了什么,我将不胜感激。

class_plus.scala:

import java.io._

class Point(val xc: Int, val yc: Int) {
   var x: Int = xc
   var y: Int = yc
     def move(dx: Int, dy: Int) {
     x = x + dx
     y = y + dy
     println ("Point x location : " + x);
     println ("Point y location : " + y);
  }
 }

  object Test {
   def main(args: Array[String]) {
      val pt = new Point(10, 20);

     // Move to a new location
     pt.move(10, 10);
   }
  }

那我运行:

  u0124587@U0124587-TPL-B ~
  $ scala class_plus.scala

  u0124587@U0124587-TPL-B ~
  $

这是因为你的main方法没有被执行。如果将此行添加到文件底部以显式调用 main 方法,它应该可以工作:

Test.main(args)

要让 Test 中的 main 方法以您期望的方式作为入口点工作而不添加额外的行,您需要先将文件编译为 class:

scalac class_plus.scala  

这将在同一目录中创建几个 class 文件,然后您可以 运行

scala Test     

这应该会为您提供您期望的输出