1.简化代码
package com.scala.first import java.io.File import javax.management.Query /** * Created by common on 17-4-5. */ object FileMatcher { def main(args: Array[String]) { for (file <- filesHere) println(file) println() for (file <- filesMatching("src", _.endsWith(_))) println(file) for (file <- filesEnding("src")) println(file) } private def filesHere = (new File(".")).listFiles //matcher是传入一个函数,返回boolean值,比如_.endsWith(_) private def filesMatching(query: String, matcher: (String, String) => Boolean) = { for (file <- filesHere; if matcher(file.getName, query)) yield file } //上面的函数不够简洁,下面是更加简洁的定义 private def filesMatch(matcher: String => Boolean) = { for (file <- filesHere; if matcher(file.getName)) yield file } //然后可以定义使用不同matcher()的方法 def filesEnding(query: String) = { filesMatch(_.endsWith(query)) } //使用exists来简化代码 def containsOdd(nums: List[Int]): Boolean = { nums.exists(_ % 2 == 1) } def containsNeg(nums: List[Int]): Boolean = { nums.exists(_ < 0) } }
输出是
./.idea ./build.sbt ./target ./input ./project ./src ./src ./src
2.柯里化
//柯里化,可以看成是两个函数,y是第一个函数sum(x: Int)的参数 def sum(x: Int)(y: Int) = { x + y } //println(sum2(2))的输出是4 def sum2 = sum(2)_
3.继承
package com.scala.first /** * Created by common on 17-4-16. */ object AbstractClass { def main(args: Array[String]): Unit = { val ae = new ArrayElement(Array("hello", "world")) println(ae.width) val list1 = List(1,2,3,4) val list2 = List(1,2,3,4,5) //Scala中同时遍历两个list,如果长度不一样会截去 for((line1,line2) <- list1 zip list2){ println(line1+line2) } } } //定义一个抽象类 abstract class Element { //定义无参方法,抽象成员 def contents: Array[String] def height: Int = contents.length def Int = if (height == 0) 0 else contents(0).length } //扩展类,继承了上面的抽象类,需要实现抽象类中的方法 class ArrayElement(content: Array[String]) extends Element { def contents: Array[String] = content } //更加简洁的写法,contents和Element中的contents保持一致 class ArrayElement2(val contents: Array[String]) extends Element //另一个例子 class cat { //确保一个成员不被子类重写,需要把其定义成final // final val dangerous = false val dangerous = false } class tiger(override val dangerous: Boolean, private var age: Int) extends cat
4.特质
package com.scala.first /** * Created by common on 17-4-17. */ object Trait { def main(args: Array[String]): Unit = { val person = new Person //变量an可以初始化为任何混入了特质的类的对象,person对象包含了Animal特质 val an: Animal = person println(an.toString) val p = new PP() //输出People特质中的内容 p.say() } } trait Animal { def run(): Unit = { println("Animal can run") } } //具有特质Animal class Person extends Animal { override def toString = "can say" } //具有特质Animal class Tiger extends Animal { override def toString = "can run fast" } //一个类只能继承一个父类。但是能混入多个特质 //特征的作用: //1.把胖接口转换成瘦接口 //2.为类提供可堆叠的改变 class Live trait HasLeg //注意不能给特质中传递任何的参数 class PersonTiger extends Live with Animal with HasLeg { println("混入了多个特质") } class P { def say(): Unit = { println("I am a people") } } //特质在抽象方法中的动态绑定 trait People extends P { abstract override def say(): Unit = { println("I am a trait people") } } class PP extends People{ //输出People特质中的内容 }