Scala,导入类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3075951/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:14:35  来源:igfitidea点击:

Scala, importing class

scalaimport

提问by matiit

I have two files:

我有两个文件:

logic.scala and main.scala

logic.scala 和 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?

logic.scala 包含一个类,而 main.scala 有一个带有方法 main(运行它)的类。我想从 logic.scala 导入一个类并使用这个类来创建对象并使用它们。
如何以正确的方式导入和编译它?

回答by Vasil Remeniuk

  • logic.scalacode
  • logic.scala代码
package logic

class Logic{

  def hello = "hello"

}
package logic

class Logic{

  def hello = "hello"

}
  • main.scalacode
  • main.scala代码
package runtime

import logic.Logic  // import

object Main extends Application{

  println(new Logic hello) // instantiation and invocation

}
package runtime

import logic.Logic  // import

object Main extends Application{

  println(new Logic hello) // instantiation and invocation

}
  • compile the files with scalac
  • 编译文件 scalac
scalac *.scala
scalac *.scala
  • run your application with scala
  • 运行您的应用程序 scala
scala -cp . runtime.Main
scala -cp . runtime.Main