scala基础备忘
声明一个变量
声明一个常量
显式指定类型
定义一个main函数
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println("hello scala") } }
定义一个普通函数
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(hello("scala")) } def hello(name : String) : String = { "hello " + name } }
函数不带参数时可以省略括号
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(hello()) } def hello() : String = { "hello scala" } }
定义一个匿名函数
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(add(1,3)) } def add = (x : Int,y : Int) => x + y }
把函数赋给一个常量
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(add(1,3)) } val add = (x : Int,y : Int) => x + y }
柯里化
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(add(1)(2)) } def add(x : Int)(y : Int) = x + y }
可变参数
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(variable("hello","admln")) } def variable(s : String*) = { s.foreach(x => println(x)) } }
参数默认值
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(theDefault()) println(theDefault("scala")) } def theDefault(s : String="admln") : String= { "hello " + s } }
判断表达式
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { val num = 5; val result = if(num > 0) 1 else 0 println(result) } }
while循环表达式
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { var (n,r) = (10,0) while(n>0) { r = r + n n = n - 1 } println(r) } }
for循环表达式
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { //1<=10 for(i <- 1 to 10) { println(i) } //1<10 for(i <- 1 until 10) { println(i) }
//只输出偶数 for(i <- 1 to 10 if i % 2 == 0 ) { println(i) } } }
创建一个类
package org.admln.scala class HelloScala { } class Person { var name : String = _ val age = 24 } object MyPerson { def main(args: Array[String]) { val p = new Person p.name = "admln" println(p.name + " " + p.age) } }
使用私有属性(外部不能访问)
package org.admln.scala class HelloScala { } class Person { var name : String = _ val age = 24 private[this] val gender = "male" } object MyPerson { def main(args: Array[String]) { val p = new Person p.name = "admln" println(p.name + " " + p.age) } }
构造器
package org.admln.scala class HelloScala { } class Person(var name : String,val age : Int){ println("main constructor") var gender : String = _ def this(name : String,age : Int,gender : String) { this(name,age) this.gender = gender } } object MyPerson { def main(args: Array[String]) { val p = new Person("admln",24,"male") println(p.name + " " + p.age) } }
继承
package org.admln.scala class HelloScala { } class Person(var name : String,val age : Int){ println("main constructor") var gender : String = _ def this(name : String,age : Int,gender : String) { this(name,age) this.gender = gender } } class Student(name : String,age : Int,val major : String) extends Person(name,age) { println("subclass is person,major is" + major) } object MyPerson { def main(args: Array[String]) { val s = new Student("admln",24,"spark") } }
覆写父类的方法和字段
package org.admln.scala class HelloScala { } class Person(var name : String,val age : Int){ println("main constructor") var gender : String = "mail" def this(name : String,age : Int,gender : String) { this(name,age) this.gender = gender } val school = "usa" } class Student(name : String,age : Int,val major : String) extends Person(name,age) { println("subclass is person,major is" + major) override def toString = "override method" override val school = "china" } object MyPerson { def main(args: Array[String]) { val s = new Student("admln",24,"spark") println(s.toString) println(s.gender) } }
抽象类
package org.admln.scala class HelloScala { } abstract class Person { def speak var name : String val age : Int } class Worker extends Person { def speak { println("speak") } var name = "admln" val age = 24 } object HelloScala extends App { val worker = new Worker worker.speak println(worker.name + " " + worker.age) }
App是trait的子类,内部实现了main方法并管理我们写的代码
trait
trait中可以带有实现的方法,也可以带有抽象方法,使用trait的方式是使用with混入类中
1 package org.admln.scala 2 3 class UseTrait { 4 5 } 6 trait Logger { 7 def log(msg : String) { 8 println("log: " + msg) 9 } 10 } 11 12 class ConcreteLogger extends Logger { 13 def concreteLog { 14 log("It's admln") 15 } 16 } 17 18 object UseTrait { 19 def main(args: Array[String]) { 20 val logger = new ConcreteLogger 21 logger.concreteLog 22 } 23 }
覆写父类的抽象方法(不用使用override关键字)
package org.admln.scala
class UseTrait {
}
trait Logger {
def log(msg : String)
}
trait ConcreteLogger extends Logger {
def log(msg : String) {
println("log: " + msg)
}
}
class Test extends ConcreteLogger {
def test {
log("It's admln")
}
}
object UseTrait {
def main(args: Array[String]) {
val test = new Test
test.test
}
}
如果子trait覆写父trait已经实现的方法,就必须使用override关键字
package org.admln.scala class UseTrait { } trait Logger { def log(msg : String) { println("log: " + msg) } } trait ConcreteLogger extends Logger { override def log(msg : String) { println(" child log: " + msg) } } class Test extends ConcreteLogger { def test { log("It's admln") } } object UseTrait { def main(args: Array[String]) { val test = new Test test.test } }
在对象中混入trait
package org.admln.scala class UseTrait { } trait Logger { def log(msg : String) { println("log: " + msg) } } trait ConcreteLogger extends Logger { override def log(msg : String) { println(" child log: " + msg) } } class Test extends Logger{ def test { log("It's admln") } } object UseTrait { def main(args: Array[String]) { val test = new Test with ConcreteLogger test.test } }
apply方法和静态方法
package org.admln.scala class UsageOfApply { } class ApplyTest { def test { println("test") } } object ApplyTest { def apply() = new ApplyTest def staticTest { println("It's static method") } } object UsageOfApply { def main(args: Array[String]) { ApplyTest.staticTest val at = ApplyTest() at.test } }
object 中的方法和属性都是静态的,所以是单例对象的理想载体
object本身就是一个单例对象
函数式编程,把运算过程尽量写成一系列嵌套的函数调用
函数的定义
def 函数名称(变量名称 : 变量类型) : 函数返回值类型 = { 函数体 }
如果函数仅包含一条语句,那么花括号可以选择不写
值函数
匿名函数
(参数名称 : 参数类型) => 表达式
把匿名函数赋值给一个常量
闭包
代码与用到的非局部变量的混合
闭包 = 代码 + 非局部变量
高阶函数map函数
下划线代表数组中的每一个元素
高阶函数filter函数
高阶函数reduce函数
scala中的集合:List、Set、Tuple、Map
它们内部都是以apply方式来完成实例化的
List和Set
和java中的Set一样,里面不会存在相同元素
Tuple
下标从1开始
Map
Option
Option代表一个可有可无的值,有两个子类:Some和None
zip操作
partition操作
flatten操作
flatmap操作:map和flatten的结合