/**
* 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
}
}
转载请注明原文地址: https://ju.6miu.com/read-1300131.html