Intellij-idea 中的 scala 类、脚本和工作表有什么区别? [英] What is the difference between scala classes, scripts and worksheets in Intellij-idea?

查看:36
本文介绍了Intellij-idea 中的 scala 类、脚本和工作表有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Intellij-idea 进行 Scala 编程(带有 sbt 插件).

I'm using Intellij-idea for scala programming (with sbt plugin).

我想知道 scala 类、scala 脚本和 scala 工作表之间有什么区别.我们什么时候使用它们?

I want to know what is the difference between scala classes, scala scripts and scala worksheets. When we use each of them?

如果你能用一个简单的例子来解释,那就太好了.

This will be very nice if you can explain it by a simple example.

谢谢

推荐答案

你有不同的方式运行scala代码:

You have different ways of running scala code:

首先用你的类创建一个程序,就像在java中一样,我使用对象,因为它不需要实例化就可以很好地工作,就像静态一样,只需使用SBT编译并运行它你也可以使用scala解释器REPL

First create a Program with your classes, this is as in java, I use object because it works well without instantianing, like static, just compile with the SBT and run it you can also use the scala Interpreter REPL

我们可以在 REPL 中使用这个对象

We can use this object in the REPL

scala>  object Hello {
     |   def main(args:Array[String]) {
     |     println("Hello, Scala !!")
     |   }
     | }
defined object Hello

scala> Hello.main(Array("onlyforwork"))
Hello, Scala !!

使用 activator/SBT 编译和运行它

compiling and running it using activator/SBT

> compile
[info] Compiling 1 Scala source to /home/anquegi/Dev/StackOverFlow/scalaStack/target/scala-2.11/classes...
[success] Total time: 2 s, completed 13/04/2015 11:29:42
> run
[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list

Multiple main classes detected, select one to run:

 [1] org.example.Hello
 [2] org.example.ScheduledTaskScala
 [3] question1.Ques
 [4] scriptworksheet.Hello

Enter number: 4

[info] Running scriptworksheet.Hello 
Hello, Scala !!
[success] Total time: 19 s, completed 13/04/2015 11:30:04

第二个是,如果我们将 scala 代码添加为脚本或文件 Hello.scala,您可以将 scala 代码保存在具有 .scala 扩展名的文件中(基本上可以使用任何文件扩展名,但首选 .scala 扩展名)并运行, 提供带有扩展名的文件名作为 scala 解释器的参数

The second is that if we add the scala code as a script or file Hello.scala, You can save your scala code in the file with .scala extension (basically with any file extension but prefered .scala extension) and to run, provide file name with extension as parameter to scala interpreter

/**
 * Created by anquegi on 13/04/15.
 */

println("Hello, Scala !!")

如果我们调用scala解释器执行这个文件,你不需要实例化对象或类,就像shell脚本一样执行,你也可以直接从Intellij执行,但是我使用系统上安装了scala的控制台

if we call the scala interpreter this file is executed, you do not need to instanciate objects or clases, just executing like a shell script, you can also execute directlyy from Intellij, but I use the console with scala installed on the system

[anquegi@localhost scalaStack]$ scala src/main/scala/scriptworksheet/HelloScript.scala 
Hello, Scala !!

最后,工作表是最强大的,我推荐这个来提高你的工作效率,因为它很容易测试就像 REPL,它会评估 scala 表达式并显示结果

And finally the worksheet is the most powerfull, I recommend this for increasing your prodductivity at work bacause it is easy to test things is like the REPL, ant it evluates the scala exprssions and shows you back the result

以下内容摘自官方 github repo wiki 关于 scala 工作表的内容

Following is excerpt from official github repo wiki about the scala worksheet

工作表是在保存时评估的 Scala 文件,每个表达式的结果显示在程序右侧的列中.工作表就像一个关于类固醇的 REPL 会话,并享受一流的编辑器支持:完成、超链接、键入时交互式错误、自动格式化等.

A worksheet is a Scala file that is evaluated on save, and the result of each expression is shown in a column to the right of your program. Worksheets are like a REPL session on steroids, and enjoy 1st class editor support: completion, hyperlinking, interactive errors-as-you-type, auto-format, etc.

// We can define objects or classes and the worksheet
//will print the sesult of every expression
object Hello {
  def main(args:Array[String]) {
    println("Hello, Scala !!")
  }
}

println("Hello Scala")
val a = 4 + 5

结果

defined module Hello





Hello Scala
res0: Unit = ()
a: Int = 9

然后是显示您在 Intellij 中使用工作表和脚本控制台的捕获

then a capture that shows you working with classe the work sheet and the console for scriptsin the Intellij

这篇关于Intellij-idea 中的 scala 类、脚本和工作表有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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