当前位置:   article > 正文

安卓Kotlin面试题 41-50

安卓Kotlin面试题 41-50
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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/221757
推荐阅读
相关标签
  

闽ICP备14008679号