Main method in Scala

后端 未结 3 1897
春和景丽
春和景丽 2020-12-09 01:29

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

3条回答
  •  既然无缘
    2020-12-09 02:06

    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:

    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)
        }
    }
    

提交回复
热议问题