概述

编程范式

Scala基础——概述 - 图1
Scala基础——概述 - 图2
Scala基础——概述 - 图3
Scala基础——概述 - 图4

函数式编程

Scala基础——概述 - 图5
就是只用纯函数(Pure function)来编写程序.
纯函数:没有副作用的函数,副作用就是状态的变化(mutation)
引用透明(Referential Transparency)对于相同的输入,总是得到相同的输出.
不变性(Immutabli)
函数是一等公民(First-class Function) 一切都是计算,函数式编程中只有比爱哦大师,变量,函数都是表达式
高阶函数(Higher order Function)
闭包(Closure)

Scala简介

官方网站
https://www.scala-lang.org/
Scala基础——概述 - 图6
运行在Java虚拟机(jvm),兼容所有的JAVA程序。
纯粹的面向对象的语言
函数式的语言

安装开发环境

下载JDK配置JAVA1.8以上开发环境

  1. $ java -version
  2. java version "1.8.0_121"
  3. Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
  4. Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

下载Scala下载地址
https://www.scala-lang.org/download/
Scala基础——概述 - 图7

  1. $ scala -version
  2. Scala code runner version 2.12.8 -- Copyright 2002-2018, LAMP/EPFL and Lightbend, Inc.

scala 历史版本

https://github.com/scala/scala/releases

idea开发IDE的安装

  1. 确保安装了 Java 8 JDK (also known as 1.8)

    • Run javac -version on the command line and make sure you seejavac 1.8.___
    • If you don’t have version 1.8 or higher, install the JDK
  2. 下载 IntelliJ Community Edition
  3. 启动 IntelliJ, 下载安装 Scala 插件,安装插件说明 how to install IntelliJ plugins (search for “Scala” in the plugins menu.)
    Scala基础——概述 - 图8

    创建工程HelloWorld

  4. 打开 IntelliJ 然后点击 File => New => Project
    2 在左侧菜单栏选择Scala 然后选择右侧选择IDEA
    Scala基础——概述 - 图9
  5. 创建项目 HelloWorld
    4 如果没有安装Scala SDK 可以选在安装SDK 版本
    Writing code
  6. On the Project pane on the left, right-click src and select New => Scala class. If you don’t see Scala class, right-click on HelloWorld and click on Add Framework Support…, select Scala and proceed. If you see Error: library is not specified, you can either click download button, or select the library path manually.
  7. Name the class Hello and change the Kind to object.
  8. Change the code in the class to the following:
  1. object Hello extends App {
  2. println("Hello, World!")
  3. }

Maven创建项目

选择org.scals-tools.archetypes:scala-archetype-simple
Scala基础——概述 - 图10

  1. <dependency>
  2. <groupId>org.scala-lang</groupId>
  3. <artifactId>scala-library</artifactId>
  4. <version>${scala.version}</version>
  5. </dependency>

REPL

REPL(Read-Eval-Print Loop,交互式解释器),为我们提供了交互式执行环境,表达式计算完成就会输出结果,而不必等到整个程序运行完毕

  1. $ scala
  2. Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121).
  3. Type in expressions for evaluation. Or try :help.
  4. scala> var x = 1
  5. x: Int = 1

使用命令“:q”退出Scala解释器

  1. scala> :q

hello world

$vim HelloWorld.scala

  1. object HelloWorld {
  2. def main(args: Array[String]){
  3. println("Hello, World!")
  4. }
  5. }

scalac命令编译

  1. $scalac HelloWorld.sacla
  2. $scala -classpath . HelloWorld