/**
* scala中任何类都有一个type属性
* 通过this.type = this这样操作,breathe即可返回Cat类型的对象,
* 因此可以进行链式操作
*/
class Animal { def breathe: this.type = this }
class Cat extends Animal { def eat: this.type = this }
object Singleton_Types {
def main(args: Array[String]): Unit = {
val cat = new Cat
cat.breathe.eat
}
}
scala进阶9-链式操作内幕
最新推荐文章于 2020-11-17 09:44:18 发布