Scala核心编程

简介

  1. Spark 新一代内存级大数据计算框架,是大数据的重要内容
  2. Scala 源代码被编译成Java字节码,所以它可以运行于JVM之上,并可以调用现有的Java类库。

Scala 特性

  • 面向对象特性
    类抽象机制的扩展有两种途径:一种途径是子类继承,另一种途径是灵活的混入机制。这两种途径能避免多重继承的种种问题。
  • 函数式编程
  • 静态类型
  • 扩展性
  • 并发性

安装

菜鸟Scala 安装
mac 安装
官网

Scala 基础语法

  • Scala 与 Java 的最大区别是:Scala 语句末尾的分号 ; 是可选的
  • 区分大小写 - Scala是大小写敏感的,这意味着标识Hello 和 hello在Scala中会有不同的含义
  • 类名 - 对于所有的类名的第一个字母要大写,示例:class MyFirstScalaClass
  • 方法名称 - 所有的方法名称的第一个字母用小写,示例:def myMethodName()
  • 程序文件名 - 程序文件的名称应该与对象名称完全匹配(新版本不需要了,但建议保留这种习惯),示例: 假设"HelloWorld"是对象的名称。那么该文件应保存为'HelloWorld.scala"
  • def main(args: Array[String]) - Scala程序从main()方法开始处理,这是每一个Scala程序的强制程序入口部分。

开始

GETTING STARTED WITH SCALA IN INTELLIJ

Scala Worksheet

它类似于交互式命令行的代码测试,在Worksheet输入scala表达式,保存以后会立即得到程序运行的结果,不用推出ideal

报错 Internal Error: null while running Scala Worksheet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Internal error: null
org.jetbrains.jps.incremental.scala.remote.ClientEventProcessor.process(ClientEventProcessor.scala:22)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.handle(RemoteResourceOwner.scala:47)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.handle$(RemoteResourceOwner.scala:37)
org.jetbrains.plugins.scala.compiler.RemoteServerRunner.handle(RemoteServerRunner.scala:16)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.$anonfun$send$5(RemoteResourceOwner.scala:30)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.$anonfun$send$5$adapted(RemoteResourceOwner.scala:29)
org.jetbrains.jps.incremental.scala.package$.using(package.scala:21)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.$anonfun$send$3(RemoteResourceOwner.scala:29)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.$anonfun$send$3$adapted(RemoteResourceOwner.scala:25)
org.jetbrains.jps.incremental.scala.package$.using(package.scala:21)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.$anonfun$send$2(RemoteResourceOwner.scala:25)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.$anonfun$send$2$adapted(RemoteResourceOwner.scala:24)
org.jetbrains.jps.incremental.scala.package$.using(package.scala:21)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.send(RemoteResourceOwner.scala:24)
org.jetbrains.jps.incremental.scala.remote.RemoteResourceOwner.send$(RemoteResourceOwner.scala:22)
org.jetbrains.plugins.scala.compiler.RemoteServerRunner.send(RemoteServerRunner.scala:16)
org.jetbrains.plugins.scala.compiler.RemoteServerRunner$$anon$1.$anonfun$run$1(RemoteServerRunner.scala:36)
scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:156)
org.jetbrains.plugins.scala.compiler.RemoteServerRunner$$anon$1.run(RemoteServerRunner.scala:32)
org.jetbrains.plugins.scala.worksheet.server.RemoteServerConnector.compileAndRun(RemoteServerConnector.scala:111)
org.jetbrains.plugins.scala.worksheet.processor.WorksheetCompiler$$anon$4.run(WorksheetCompiler.scala:81)
com.intellij.compiler.progress.CompilerTask.run(CompilerTask.java:185)
com.intellij.openapi.progress.impl.CoreProgressManager$TaskRunnable.run(CoreProgressManager.java:727)
com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:164)
com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:582)
com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:532)
com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:87)
com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:151)
com.intellij.openapi.progress.impl.CoreProgressManager$4.run(CoreProgressManager.java:403)
com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:314)
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

解决办法:
settings of the worksheet from <Run type: REPL> into <Run type:plain> and it worked.
Internal Error: null while running Scala Worksheet

1
2
3
4
5
6
7
// object表示一个伴生对象,可以理解就是一个对象
// Hello就是对象名字,它底层真正对应的类名是Hello$
object Hello {
def main(args: Array[String]): Unit = {
print("hello world")
}
}
1
2
3
object Hello extends App {
print("hello world")
}