Scala 脚本中未调用 Main 方法 [英] Main method is not called in Scala script

查看:59
本文介绍了Scala 脚本中未调用 Main 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scala 的新手,我想知道为什么 main 方法没有在这个脚本中运行?

I am brand new to Scala, and I was wondering why the main method is not running in this script?

class Word {
}

object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")
  }
}

有趣的是,当我删除 Word 类时它工作正常.为什么是这样?我该如何解决?

The interesting thing is that it works fine when i remove the Word class. Why is this? And how do I fix it?

推荐答案

scala入门所示,当将 Scala 文件作为脚本运行时,您应该显式调用 main 方法:

As shown in getting started with scala, you should explicitly invoke the main method, when running scala files as a script:

class Word {
}

object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")
  }
}

HelloWorld.main(args)

如果你想编译和运行,你必须给出包含 main 方法的对象的名称:

If you want to compile and run, you'll have to give the name of the object holding the main method:

scalac test.scala
scala HelloWorld

这篇关于Scala 脚本中未调用 Main 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆