表达式,值,变量,代码块,函数,方法

// 表达式
1 + 1
println(1 + 1) // 2
// 值(values)使用 val 关键字声明,带初值时类型可省略。
val x = 1 + 1
println(x) // 2
val x: Int = 1 + 1
// x = 3
// 变量(variables)使用 var 关键字声明,带初值时类型可省略。
var x = 1 + 1
var x: Int = 1 + 1
x = 3
// 代码块(blocks): 使用大括号括起来,代码块的值由最后一句表达式语句的值来决定。
println({
val x = 1 + 1
x + 1
}) // 3
// 函数(functions): 参数类型不可省略,返回值的类型可省略。
(x: Int) => x + 1
val addOne = (x: Int) => x + 1
println(addOne(1)) // 2
// 函数可以有多个参数
val add = (x: Int, y: Int) => x + y
println(add(1, 2)) // 3
// 函数也可以没有参数
val add = (x: Int, y: Int) => x + y
println(add(1, 2)) // 3
// 方法(methods): 使用 def 关键字声明,参数类型不可省略,返回值的类型可省略。
def add(x: Int, y: Int): Int = x + y
println(add(1, 2)) // 3
// 方法可以有多个参数列表
def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier
println(addThenMultiply(1, 2)(3)) // 9
// 方法也可以没有参数
def name: String = System.getProperty("name")
println("Hello, " + name + "!")
// 方法的值取决于最后一句表达式语句的值
def getSquareString(input: Double): String = {
val square = input * input
square.toString
}

类,样例类,单例对象,特质,Main方法

// 类(classes)的定义
class Greeter(prefix: String, suffix: String) {
def greet(name: String): Unit =
println(prefix + name + suffix)
}
// 用关键字 new 创建类的实例
val greeter = new Greeter("Hello, ", "!")
greeter.greet("Scala developer") // Hello, Scala developer!
// 样例类(case classes)
case class Point(x: Int, y: Int)
// 创建样例类的实例不用 new
val point = Point(1, 2)
val anotherPoint = Point(1, 2)
// 样例类的实例通过值来比较
if (point == anotherPoint) {
println(point + " and " + anotherPoint + " are the same.")
} else {
println(point + " and " + anotherPoint + " are different.")
}
// Point(1,2) and Point(1,2) are the same.
// 单例对象
object IdFactory {
private var counter = 0
def create(): Int = {
counter += 1
counter
}
}
// 通过对象的名字来调用它的方法
val newId: Int = IdFactory.create()
println(newId) // 1
val newerId: Int = IdFactory.create()
println(newerId) // 2
// 特质(traits)
trait Greeter {
def greet(name: String): Unit
}
trait Greeter {
def greet(name: String): Unit =
println("Hello, " + name + "!")
}
// 用类来扩展特质
class DefaultGreeter extends Greeter
class CustomizableGreeter(prefix: String, postfix: String) extends Greeter {
override def greet(name: String): Unit = {
println(prefix + name + postfix)
}
}
val greeter = new DefaultGreeter()
greeter.greet("Scala developer") // Hello, Scala developer!
val customGreeter = new CustomizableGreeter("How are you, ", "?")
customGreeter.greet("Scala developer") // How are you, Scala developer?
// Main 方法(在单例对象中定义)
object Main {
def main(args: Array[String]): Unit =
println("Hello, Scala developer!")
}

类型层次

Any 类型是所有类型的超类型。

AnyVal 类型代表值类型。

AnyRef 类型代表引用类型。

Nothing 类型是所有类型的子类型。该类型只有一个实例: null。

val list: List[Any] = List(
"a string",
732, // an integer
'c', // a character
true, // a boolean value
() => "an anonymous function returning a string"
)
list.foreach(element => println(element))

Scala语言学习笔记(2)的更多相关文章

  1. Scala语言学习笔记——方法、函数及异常

    1.Scala 方法及函数区别 ① Scala 有方法与函数,二者在语义上的区别很小.Scala 方法是类的一部分,而函数是一个对象可以赋值给一个变量.换句话来说在类中定义的函数即是方法 ② Scal ...

  2. Scala语言学习笔记(3)

    类 // 定义并使用类 class User val user1 = new User // 主体构造器(primary constructor) class Point(var x: Int, va ...

  3. Scala语言学习笔记(4)

    高阶函数 // 高阶函数 map val salaries = Seq(20000, 70000, 40000) val doubleSalary = (x: Int) => x * 2 val ...

  4. HTML语言学习笔记(会更新)

    # HTML语言学习笔记(会更新) 一个html文件是由一系列的元素和标签组成的. 标签: 1.<html></html> 表示该文件为超文本标记语言(HTML)编写的.成对出 ...

  5. Scala入门学习笔记三--数组使用

    前言 本篇主要讲Scala的Array.BufferArray.List,更多教程请参考:Scala教程 本篇知识点概括 若长度固定则使用Array,若长度可能有 变化则使用ArrayBuffer 提 ...

  6. 2017-04-21周C语言学习笔记

    C语言学习笔记:... --------------------------------- C语言学习笔记:学习程度的高低取决于.自学能力的高低.有的时候生活就是这样的.聪明的人有时候需要.用笨的方法 ...

  7. 2017-05-4-C语言学习笔记

    C语言学习笔记... ------------------------------------ Hello C语言:什么是程序:程序是指:完成某件事的既定方式和过程.计算机中的程序是指:为了让计算机执 ...

  8. GO语言学习笔记(一)

    GO语言学习笔记 1.数组切片slice:可动态增长的数组 2.错误处理流程关键字:defer panic recover 3.变量的初始化:以下效果一样 `var a int = 10` `var ...

  9. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

随机推荐

  1. windows7 64位下git和tortoisegit的安装和使用

    githttps://github.com/git-for-windows/git/releases tortoisegit安装下载https://tortoisegit.org/download/ ...

  2. Ionic 中MD5加密使用

    1. 下载安装ts-md5 在项目的命令行工具里输入 npm  install ts-md5 --save 2. 使用 导入 import {Md5} from "ts-md5/dist/m ...

  3. Ionic Tabs使用

    1. 创建Tabs相关页面 ionic g page tabs ionic g page TabOne ionic g page TabTwo ionic g page TabThree 2. 在ta ...

  4. AngularJs $scope 里面的$apply 方法和$watch方法

    Angular $scope 里面的$apply 方法 Scope提供$apply方法传播Model变化 <!DOCTYPE html> <html> <head> ...

  5. Go随机数的使用

    随机数使用比较广泛,例如,抽奖.均衡等等. 下面简单说明其使用方法. Example1 package main import ( "log" "math/rand&qu ...

  6. Hanlp中文自然语言处理入门介绍

    自然语言处理定义: 自然语言处理是一门计算机科学.人工智能以及语言学的交叉学科.虽然语言只是人工智能的一部分(人工智能还包括计算机视觉等),但它是非常独特的一部分.这个星球上有许多生物拥有超过人类的视 ...

  7. 中点Brehensam画圆算法

    #include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<math.h> v ...

  8. ASP.NET Web Pages:Razor

    ylbtech-.Net-ASP.NET Web Pages:Razor 1.返回顶部 1. ASP.NET Web Pages - 添加 Razor 代码 在本教程中,我们将使用 C# 和 Visu ...

  9. CentOS下安装robot-framework

    http://www.cnblogs.com/hengwei/p/5464678.html http://www.2cto.com/kf/201405/305383.html https://pypi ...

  10. 1115 Counting Nodes in a BST (30 分)

    1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...