Scala, importing class

2019-02-05 15:41发布

问题:

I have two files:

logic.scala and main.scala

logic.scala contains one class and main.scala have one class with method main (to run it). And I want to import a class from logic.scala and use this class to create object(s) and work with them.
How to import and compile it in proper way?

回答1:

  • logic.scala code
package logic

class Logic{

  def hello = "hello"

}
  • main.scala code
package runtime

import logic.Logic  // import

object Main extends Application{

  println(new Logic hello) // instantiation and invocation

}
  • compile the files with scalac
scalac *.scala
  • run your application with scala
scala -cp . runtime.Main


标签: scala import