Scala 中的主要方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23416536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Main method in Scala
提问by user2327621
I wrote a Scala class and defined the main()method in it. It compiled, but when I ran it, I got NoSuchMethodError:main. In all the scala examples, I have seen, the main method is defined in an object. In Java we define the main method in a class. Is it possible to define main()in a Scala class or do we always need an object for this?
我编写了一个 Scala 类并main()在其中定义了方法。它编译了,但是当我运行它时,我得到了NoSuchMethodError:main. 在我看到的所有 Scala 示例中,主要方法都定义在一个对象中。在 Java 中,我们在类中定义 main 方法。是否可以main()在 Scala 类中定义,或者我们是否总是需要一个对象?
回答by Apoorv Verma
To answer your question, have a look on the following : I made a scala class, compiled and decompiled it, and what I got is interesting.
要回答您的问题,请查看以下内容:我制作了一个 Scala 类,对其进行了编译和反编译,结果很有趣。
class MyScalaClass{
def main(args: Array[String]): Unit = {
println("Hello from main of class")
}
}
Compiled from "MyScalaClass.scala"
public class MyScalaClass {
public void main(java.lang.String[]);
public MyScalaClass();
}
So it means that when the scala class is converted to java class then the main method of the scala class which in turn being converted to the main method in java class is not static. And hence we would not be able to run the program because JVM is not able to find the starting point in the program.
所以这意味着当 scala 类转换为 java 类时,scala 类的 main 方法又转换为 java 类中的 main 方法不是静态的。因此我们将无法运行程序,因为 JVM 无法在程序中找到起点。
But if the same code is done by using the 'object'keyword then:
但是,如果使用'object'关键字完成相同的代码,则:
Compiling the following:
object MyScalaClass{
def main(args: Array[String]): Unit = {
println("Hello from main of object")
}
}
Decompiling the following:
javap MyScalaClass$.class
Compiled from "MyScalaClass.scala"
public final class MyScalaClass$ {
public static final MyScalaClass$ MODULE$;
public static {};
public void main(java.lang.String[]);
}
Decompiling the following
javap MyScalaClass.class
Compiled from "MyScalaClass.scala"
public final class MyScalaClass {
public static void main(java.lang.String[]);
}
So here we gotpublic static void main in MyScalaClass.classtherefore the main method can be executed directly by the JVM here.
所以这里我们在 MyScalaClass.class 中得到了public static void main,因此这里的 main 方法可以由 JVM 直接执行。
I hope you got your answer.
我希望你得到你的答案。
回答by AmigoNico
As Eugene said in a comment, there are no static methods in Scala. But watch this:
正如 Eugene 在评论中所说,Scala 中没有静态方法。但请注意:
$ cat Echo.scala
object Echo {
def main( args:Array[String] ):Unit = args foreach println
}
$ scalac Echo.scala
$ javap Echo$.class
Compiled from "Echo.scala"
public final class Echo$ {
public static final Echo$ MODULE$;
public static {};
public void main(java.lang.String[]);
}
$ javap Echo.class
Compiled from "Echo.scala"
public final class Echo {
public static void main(java.lang.String[]);
}
Note that the classfile for the Echo class (not Echo$, the object) does indeed have a public static void main method. Scala generates static methods for methods defined in objects for compatibility with Java.
请注意,Echo 类(不是 Echo$,对象)的类文件确实有一个 public static void main 方法。Scala 为对象中定义的方法生成静态方法以与 Java 兼容。
However, I consider creating a main method in a Scala program an anachronism. Use the App trait instead; it's cleaner:
但是,我认为在 Scala 程序中创建一个 main 方法是不合时宜的。改用 App 特性;它更干净:
object Echo extends App {
args foreach println
}
回答by abshar
When I want to test my code in intelligent idea scala editor, I simply create a companion object just below my class and put a main method in it. That's all. see an example:
当我想在智能想法 scala 编辑器中测试我的代码时,我只需在我的类下面创建一个伴随对象并在其中放置一个 main 方法。就这样。看一个例子:
class Colon {
class Cow {
def ^ (moon:Moon): Unit ={
println("Cow jumped over the moon")
}
}
class Moon{
def ^:(cow:Cow) = println("This cow jumped over moon too")
}
}
object Colon{
def main(args: Array[String]): Unit = {
val c:Colon = new Colon
val cow = new c.Cow
val moon = new c.Moon
cow ^ moon
cow ^: moon
moon.^:(cow)
}
}

