知识点记录-SCALA语言
目录
//package import
package com.scala
import java.io._
import java.util.Date
import scala.io.Source
import scala.util.control.Breaks
import scala.util.matching.Regex
object Test {
def main(args: Array[String]): Unit = {
// 注释
/* 多行注释 */
/**
* 文档类注释
* */
println("Hello\tHello1\nHello2\n")
// 变量
var v1: String = "变量字符串1"
val v2: String = "常量字符串2"
println(v1);
println(v2)
val v3, v4: Int = 5 //声明多个变量
println("v3=" + v3 + " v4=" + v4)
var v5 = (5, "5") //tuple
println("v5=" + v5)
// 访问修饰符
//private,protected,public(默认)
class Outer {
class Inner {
private def f() {
println("f")
}
def f1() {
println("f1")
}
class InnerInner {
f() // 正确
f1() // 正确
private def f(): Unit = {
f() // 正确
}
def f1(): Unit = {
f1() // 正确
}
}
}
// (new Inner).f() //错误
(new Inner).f1() // 正确
}
class Super {
protected def f() {
println("f")
}
}
class Sub extends Super {
f()
}
class Other {
// (new Super).f() //错误
}
// 作用域
//作用域 private[x] 或 protected[x] 在x表示的包 类 或object中可见,外部是private
// package package1 {
// package package2 {
// private[package1] class Navigator { //Navigator在package1包可见
// protected[package2] def useStarChart() {}
// class LegOfJourney {
// private[Navigator] val distance = 100
// }
// private[this] var speed = 200
// }
// }
// package launch {
// object Vehicle {
// private[launch] val guide = new Navigator //launch在package1内可引用Navigator
// }
// }
// }
// 运算符
// 运算符
var a = 10;
var b = 20;
println("a + b = " + (a + b));
println("a - b = " + (a - b));
println("a * b = " + (a * b));
println("b / a = " + (b / a));
println("b % a = " + (b % a));
println("a == b = " + (a == b));
println("a != b = " + (a != b));
println("a > b = " + (a > b));
println("a < b = " + (a < b));
println("b >= a = " + (b >= a));
println("b <= a = " + (b <= a));
println("a & b = " + (a & b));
println("a | b = " + (a | b));
println("a ^ b = " + (a ^ b));
println("~a = " + (~a));
println("a << 2 = " + (a << 2));
println("a >> 2 = " + (a >> 2));
println("a >>> 2 = " + (a >>> 2));
a += b
println("a+=b=" + a)
var c = true;
var d = false;
println("c && d = " + (c && d));
println("c || d = " + (c || d));
println("!(c && d) = " + !(c && d));
if (a >= 0) {
println("a >= 0")
}
else println("a < 0")
// 循环
// 循环while do-while for
for (a <- 1 to 2) {
println("to a=" + a)
}
for (a <- 1 until 2) {
println("until a=" + a)
}
for (a <- 1 to 2; b <- 3 to 4) {
println("Value of a: " + a);
println("Value of b: " + b);
}
for (a <- List(1, 2)) {
println("List of a: " + a);
}
for (a <- List(1, 2); if a < 2; if a >= 0) {
println("List of a: " + a);
}
// yeild
// yeild 用retVal保存yeild值的集合返回
val retVal = for {a <- List(1, 3, 9, 5) if a != 3; if a < 8} yield a
for (a <- retVal) {
println("retVal: " + a);
}
// break
//break
val outer = new Breaks;
val inner = new Breaks;
val numList1 = List(1, 2, 3, 4, 5);
val numList2 = List(11, 12, 13);
outer.breakable {
for (a <- numList1) {
println("break of a: " + a);
if (a == 2) outer.break()
inner.breakable {
for (b <- numList2) {
println("break of b: " + b);
if (b == 12) {
inner.break;
}
}
} // 内嵌循环中断
}
} // 外部循环中断
// 方法与函数
def m(x: Int) = x + 2 //方法
val f = (x: Int) => x + 3 //函数
println("m(1)=" + m(1))
println("f(1)=" + f(1))
//方法转函数
val f1 = m _
println("f1(1)=" + f1(1))
def addInt(a: Int = 1, b: Int = 2): Int = {
// var sum: Int = 0
// sum = a + b
// return sum
//或
// var sum: Int = 0
// sum = a + b
// sum
//或
// return a + b
//或
println("addInt a=" + a)
println("addInt b=" + b)
a + b
}
//方法调用
println("方法调用addInt(5,6)=" + addInt(5, 6))
//参数名调用
println("参数名调用 addInt(5,6)=" + addInt(b = 5, a = 6))
// 默认参数
//默认参数
println("默认参数 addInt=" + addInt())
//默认参数
println("默认参数 addInt(3)=" + addInt(3))
// 高阶函数
// 高阶函数
// 函数 f 和 值 v 作为参数,而函数 f 又调用了参数 v
def apply(f: Int => String, v: Int) = f(v)
def layout[A](x: A) = "[" + x.toString() + "]"
println("高阶函数=" + apply(layout, 500))
//传名调用 将未计算的参数表达式直接应用到函数内部
//传值调用 先计算参数表达式的值,再应用到函数内部
def time() = {
println("获取时间,单位为纳秒")
System.nanoTime
}
def delayed(t: => Long) = {
println("在 delayed 方法内")
println("参数: " + t)
t
}
delayed(time())
// 可变参数
def printStrings(args: String*) = {
var i: Int = 0;
for (arg <- args) {
println("可变参数[" + i + "] = " + arg);
i = i + 1;
}
}
//可变参数
printStrings("String1", "String2")
// 递归函数
//递归函数
def factorial(n: BigInt): BigInt = {
if (n <= 1)
1
else
n * factorial(n - 1)
}
println("递归函数3的阶乘=" + factorial(3))
// 内嵌函数
//内嵌函数
def factorial1(i: Int): Int = {
def fact(i: Int, accumulator: Int): Int = {
if (i <= 1)
accumulator
else
fact(i - 1, i * accumulator)
}
fact(i, 1)
}
println("内嵌函数5的阶乘=" + factorial1(5))
// 匿名函数
// 匿名函数
val add1 = (x: Int, y: Int) => x + y
def add2 = new Function2[Int, Int, Int] {
def apply(x: Int, y: Int): Int = x + y;
}
println("匿名函数add1(1,2)=" + add1(1, 2))
println("add2(1,2)=" + add2(1, 2))
// 偏函数
// 偏函数
val date = new Date
def log(date: Date, message: String) = {
println(date + "----" + message)
}
val logWithDateString = log(date, _: String)
logWithDateString("message1")
Thread.sleep(500)
logWithDateString("message2")
Thread.sleep(200)
logWithDateString("message3")
// 柯里化函数
//柯里化函数
def add3(x: Int)(y: Int) = x + y
println("柯里化函数add3(1)(2)=" + add3(1)(2))
// 闭包
//闭包
var factor = 5
val add5 = (i: Int) => i + factor
println("包add5(5)=" + add5(6))
// 数组
//数组
var array1 = Array("array0", "array1")
for (a <- array1) {
println("数组array1=" + a)
}
var array2 = Array.ofDim[Int](2, 3)
for (i <- 0 until (2)) {
for (j <- 0 until (3)) {
array2(i)(j) = (j)
}
}
for (i <- 0 until (2)) {
for (j <- 0 until (3)) {
println("数组array2=" + array2(i)(j))
}
}
val array3 = Array.range(5, 9, 3)
for (a <- array3) {
println("数组array3=" + a)
}
// 集合
//集合
val list = List(1, 2)
val set = Set(1, 3)
val map = Map("one" -> 1, "two" -> 2)
val tuple = (1, "string")
val option: Option[Int] = Some(5)
val seq = Seq(5, 6)
println(s"The max seg=${seq.max}")
var iteratorList = list.iterator
while (iteratorList.hasNext) {
println("iterator List=" + iteratorList.next())
}
iteratorList = list.iterator
println("iterator max=" + iteratorList.max)
iteratorList = list.iterator
println("iterator min=" + iteratorList.min)
iteratorList = list.iterator
println("iterator size=" + iteratorList.size)
iteratorList = list.iterator
println("iterator length=" + iteratorList.length)
// 文件
//文件
val file = new File("file.txt")
val writer = new PrintWriter(file)
writer.write("文件1")
writer.write("\n")
writer.write("文件2")
writer.close()
Source.fromFile(file).foreach(print)
println
// 异常
//异常
try {
val f = new FileReader("input.txt")
} catch {
case ex: FileNotFoundException => {
println("FileNotFoundException")
}
case ex: IOException => {
println("IOException")
}
} finally {
println("finally")
}
// println("屏幕IO input:")
// val line = StdIn.readLine()
// println("屏幕IO input=" + line)
// regex
//regex
val pattern1 = "Scala".r
val patternStr = "Scala is scalable and cool"
println(pattern1 findFirstIn patternStr)
val pattern2 = new Regex("(S|s)cala")
println((pattern2 findAllIn patternStr).mkString(","))
println(pattern2.replaceFirstIn(patternStr, "SCA"))
// 模式匹配
// 模式匹配
def matchTest(x: Any): Any = x match {
case 1 => "one"
case 2 => "two"
case y: Int => "scala.Int"
case "two" => 2
case _ => "many"
}
println(matchTest(3))
println(matchTest("two"))
println(matchTest("test"))
println(matchTest(1))
println(matchTest(6))
}
}