scala - yaokun123/php-wiki GitHub Wiki

分号可有可无 scala是包级别区分,类名可以和文件名不一致

https://docs.scala-lang.org/zh-cn/tour/tour-of-scala.html

一、class 和 object关键词

class:
1、类里,裸露的代码是默认构造中的。有默认构造
2、个性化构造!!
3、类名构造器中的参数就是类的成员属性,且默认是val(常量)类型,且默认是private
4、只有在类名构造其中的参数可以设置成var(变量),其他方法函数中的参数都是val(常量)类型的,且不允许设置成var类型

object:
1、约等于  static  单例对象
2、单例  new    scala的编译器很人性化   让你人少写了很多代码

二、循环

// if
    var a=0
    if(a <= 0){
      println(s"$a<0")
    }else{
      println(s"$a>=0")
    }

// while
    var  aa=0
    while(aa <10){
      println(aa)
      aa=aa+1
    }

// for
//    for(i=0;i<10;i++)
//    for( P x : xs)

    val seqs = 1 until  10
    println(seqs)

    //循环逻辑,业务逻辑
    for( i <-  seqs if(i%2==0)){
      println(i)
    }

    // 双重循环-九九乘法表
    var num = 0
    for(i <- 1 to 9;j <- 1 to 9 if(j<=i)){
      num+=1
        if(j<=i) print(s"$i * $j = ${i*j}\t")
        if(j == i ) println()
    }
    println(num)


    // 迭代器
    val seqss: immutable.IndexedSeq[Int] = for ( i <- 1 to 10) yield {
      var x = 8
      i + x
    }
    println(seqss)
    for(i <-seqss){
      println(i)
    }

三、函数

// 成员方法,冒号后面是返回值类型
def ooxx(): Unit ={
    println("hello object")
}

// 有return必须给出返回类型
def fun02():util.LinkedList[String] = {
      new util.LinkedList[String]()
}

// 参数:必须给出类型,是val(常量)
// class 构造,是var,val
def fun03(a: Int): Unit = {
      println(a)
}
fun03(33)

// 递归函数
def fun04(num: Int): Int = {
      if (num == 1) {// 递归先写触底
        num
      } else {
        num * fun04(num - 1)
      }
}

val i: Int = fun04(4)
println(i)

// 默认值函数
def fun05(a: Int = 8, b: String = "abc"): Unit = {
      println(s"$a\t$b")
}

//    fun05(9,"def")
fun05(22)
fun05(b = "ooxx")

// 匿名函数
var xx: Int = 3
var yy: (Int, Int) => Int = (a: Int, b: Int) => {
    a + b
}
val w: Int = yy(3, 4)
println(w)

// 嵌套函数
def fun06(a: String): Unit = {
    def fun05(): Unit = {
        println(a)
    }
    fun05()
}
fun06("hello")

// 偏应用函数
def fun07(date: Date, tp: String, msg: String): Unit = {
      println(s"$date\t$tp\t$msg")
}
fun07(new Date(), "info", "ok")
var info = fun07(_: Date, "info", _: String)
var error = fun07(_: Date, "error", _: String)
info(new Date, "ok")
error(new Date, "error...")

// 可变参数
def fun08(a: Int*): Unit = {
      for (e <- a) {
        println(e)
      }
      //      def foreach[U](f: A => U): Unit
      //      a.foreach(   (x:Int)=>{println(x)}   )
      //      a.foreach(   println(_)   )
      a.foreach(println)
}
fun08(2)
fun08(1, 2, 3, 4, 5, 6)

// 高阶函数 - 函数作为参数,函数作为返回值
def computer(a: Int, b: Int, f: (Int, Int) => Int): Unit = {// 函数作为参数
      val res: Int = f(a, b)
      println(res)
}
    computer(3, 8, (x: Int, y: Int) => {
      x + y
    })
    computer(3, 8, (x: Int, y: Int) => {
      x * y
    })
    computer(3, 8, _ * _)
def factory(i: String): (Int, Int) => Int = {// 函数作为返回值
      def plus(x: Int, y: Int): Int = {
        x + y
      }
      if (i.equals("+")) {
        plus
      } else {
        (x: Int, y: Int) => {
          x * y
        }
      }
}

// 柯里化
def fun09(a: Int)(b: Int)(c: String): Unit = {
      println(s"$a\t$b\t$c")
}
fun09(3)(8)("sdfsdf")
def fun10(a: Int*)(b: String*): Unit = {
      a.foreach(println)
      b.foreach(println)
}
fun10(1, 2, 3)("sdfs", "sss")

// 方法 
// 方法不想执行,赋值给一个引用  方法名+空格+下划线
val funa = ooxx
println(funa)
val func = ooxx _
func()