我们在使用spark的时候 开头经常写这样的fluent style代码
val conf = new SparkConf().setAppName("ParallelizeCollection").setMaster("local")
那我们自己怎样然我们自己的代码能实现这样的效果,其实想要使用流式风格
1、如果你的类会被继承的话 方法返回类型必须是this.type
2、如果你的类不会被继承的话 方法return是this就可以
object FluentStyleScala { def main(args: Array[String]): Unit = { /** * A fluent style of programming lets users of your API write code by chaining method calls together * 想要使用流式风格 1、如果你的类会被继承的话 方法返回类型必须是this.type 2、如果你的类不会被继承的话 方法return是this就可以 */ val e = new Worker().setAge(10).setName("jian").setRole("Developer") println(e) val p = new Pizza() .setCrustSize(14) .setCrustType("thin") .addTopping("cheese") .addTopping("green olives") .print() } } class Emplory{ protected var name = None:Option[String] protected var age = None:Option[Int] def setName(name:String):this.type = { this.name = Some(name) this } def setAge(age:Int):this.type = { this.age = Some(age) this } override def toString() = s"[name] = $name [age] = $age" } class Worker extends Emplory{ protected var role = "" def setRole(role: String): this.type = { this.role = role this } override def toString = { "%s, %d, %s".format(name.get, age.get, role) } } final class Pizza{ import scala.collection.mutable.ArrayBuffer private val toppings = ArrayBuffer[String]() private var crustSize = 0 private var crustType = "" def addTopping(topping: String) = { toppings += topping this } def setCrustSize(crustSize: Int) = { this.crustSize = crustSize this } def setCrustType(crustType: String) = { this.crustType = crustType this } def print() { println(s"crust size: $crustSize") println(s"crust type: $crustType") println(s"toppings: $toppings") } }