问题描述
IntelliJ IDEA 10.5(可能这是重要的)。
IntelliJ IDEA 10.5 (probably this matters).
我是Scala的新人,所以我开始以一种akward的方式。我创建了一个文件与两个类 - 空MainApp和另一个类,HelloWorld与方法main。
I am new to Scala, so I started in an akward way. I created one file with two classes -- empty MainApp and another class, HelloWorld with method main.
我编译并执行IntelliJ自动检测HelloWorld作为主类。
I compiled it and executed -- IntelliJ automatically detected HelloWorld as main class. It was OK.
然后,我将main方法移动到MainApp,并删除(然后清空)HelloWorld类。当我试图运行它,IntelliJ坚持到HelloWorld。所以我重新配置项目并选择MainApp作为主类。
Then, I moved main method to MainApp, and deleted (then empty) HelloWorld class. When I tried to run it, IntelliJ sticked to HelloWorld nevertheless. So I reconfigured project and selected MainApp as main class.
我试图用这样的结果运行它:
I tried to run it with such result:
我完全感到困惑。首先,Scala没有静态方法。第二,为什么它不编译现在,当它编译之前(与HelloWorld类)。
I am completely puzzled. First of all, Scala does not have static methods. Second of all, why it does not compile now, when it compiled before (with HelloWorld class). I though that only requirement is having one main method.
预先感谢您的帮助。
推荐答案
Java中的静态方法大致对应于Scala中的单例方法。您应该有
static methods in Java roughly correspond to singleton methods in Scala. You should have
object MainApp {
def main(args : Array[String]) = ...
}
,而不是 class MainApp
。
这篇关于如何将主要方法移动到另一个类在Scala?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!