/**
* 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-链式操作内幕
最新推荐文章于 2024-10-03 17:00:00 发布
本文介绍Scala中通过this.type实现链式调用的方法。以Cat类为例,展示了如何定义breathe和eat方法,并使它们能够返回当前对象类型,从而允许连续调用这些方法。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Stable-Diffusion-3.5
图片生成
Stable-Diffusion
Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率
737

被折叠的 条评论
为什么被折叠?



