Scala,导入类 [英] Scala, importing class

查看:171
本文介绍了Scala,导入类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件:



logic.scala和main.scala



logic.scala包含一个类,main.scala有一个类,方法为main(运行它)。我想从logic.scala导入一个类,并使用这个类来创建对象并使用它们。

如何以适当的方式导入和编译它?

解决方案


  • logic.scala code




 包逻辑

class Logic {

def hello =hello

}





  • main.scala 代码




 包运行时

import logic.Logic // import

object Main扩展Application {

println(new Logic hello)//实例化和调用

}





  • 使用 scalac编译文件




  scalac * .scala 





  • 使用 scala




  scala  - cp。 runtime.Main 



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?

解决方案

  • 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,导入类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆