0
点赞
收藏
分享

微信扫一扫

计算机网络 网络层设备的 冲突域和广播域

c一段旅程c 2024-03-12 阅读 6
41、如何在 Kotlin 中实现 Builder 模式?
首先,在大多数情况下,您不需要在 Kotlin 中使用构建器,因为我们有默认和命名参数,但如果您需要使用:

//add private constructor if necessary

class Car( val model: String?,val year: Int) {

        private constructor(builder: Builder) : this(builder.model, builder.year)

        class Builder {
                var model: String? = null
                private set

                var year: Int = 0
                private set

                fun model(model: String) = apply {

                        this.model = model

                 }

                fun year(year: Int) = apply {

                        th

举报

相关推荐

0 条评论