Scala 运行菜单项在IntelliJ中消失
我在IntelliJ中有如下scala代码:Scala 运行菜单项在IntelliJ中消失,scala,intellij-idea,Scala,Intellij Idea,我在IntelliJ中有如下scala代码: HelloWorld.scala object HelloWorld { //var matchExample = new MatchExample() def main(args: Array[String]) = { printHello() //matchExample.oddEven(1) } def printHello() = { println("hello") } } class M
HelloWorld.scala
object HelloWorld {
//var matchExample = new MatchExample()
def main(args: Array[String]) = {
printHello()
//matchExample.oddEven(1)
}
def printHello() = {
println("hello")
}
}
class MatchExample {
def oddEven(x: Int): String = {
"Hello"
}
}
MatchExample.scala
object HelloWorld {
//var matchExample = new MatchExample()
def main(args: Array[String]) = {
printHello()
//matchExample.oddEven(1)
}
def printHello() = {
println("hello")
}
}
class MatchExample {
def oddEven(x: Int): String = {
"Hello"
}
}
若我取消注释这两行,并尝试通过右键单击对象来运行,我并没有运行菜单项,但若我注释掉这两行,那个么我有“运行”菜单项
我缺少什么?原因是您的主方法签名(un-comment-matchExample.oddeen(1))与Scala编译器对可运行程序的要求不兼容 您的是(args:Array[String])字符串,可运行程序的主要方法签名是(args:Array[String])单位。若在终端中运行代码,编译器将发出警告
allen:Desktop allen$ scalac HelloWorld.scala
HelloWorld.scala:1: warning: HelloWorld has a main method with
parameter type Array[String], but HelloWorld will not be a runnable program.
Reason: main method must have exact signature (Array[String])Unit
object HelloWorld {
^
one warning found
在Scala中,如果您想编写一个可运行的程序,最好使用App trait。
这篇文章详细解释了这一点