Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套成员的Kotlin可见性_Kotlin - Fatal编程技术网

嵌套成员的Kotlin可见性

嵌套成员的Kotlin可见性,kotlin,Kotlin,我有一个嵌套的私有类。我有一个Builder,标准的JavaBuilder模式,它构造这个类的实例。我不希望班外的任何人都能看到我隐藏的班级 在Java中,我可以这样做: public class Example { private SneakyType doNotExposeThis; private Example(Builder builder) { // OK 'cause in Java you can access the private

我有一个嵌套的私有类。我有一个
Builder
,标准的JavaBuilder模式,它构造这个类的实例。我不希望班外的任何人都能看到我隐藏的班级

在Java中,我可以这样做:

public class Example {
    private SneakyType doNotExposeThis;

    private Example(Builder builder) {
        // OK 'cause in Java you can access the private
        // members of a nested class
        doNotExposeThis = builder.doNotExposeThis;
    }

    private static class SneakyType {
        SneakyType(String x) {
            // stuff
        }
    }

    public static class Builder {
        private SneakyType doNotExposeThis;

        public void addFoo(String something) {
            doNotExposeThis = new SneakyType(something);
        }

        public Example build() { return new Example(this); }
    }
}
但我不知道如何在Kotlin做到这一点:

class Example(builder: Builder) {
    private lateinit var doNotExposeThis: SneakyType

    init {
        doNotExposeThis = builder.doNotExposeThis
    }

    class Builder {
        // If private or internal I can't access it in Example.init
        // and if public it gets exposed. 
        val doNotExposeThis: SneakyType


        fun addFoo(something: String) {
            // actual construction is quite a bit more complex
            doNotExposeThis = SneakyType(something)
        }
    }
}
请注意,为了实现Java互操作,我希望保留我的生成器。我还想要它,因为我的对象构造起来很复杂,而且我希望它是不可变的,所以我有一个具有大量setter、adder、vals等的生成器,然后在
init
中,我构造了一个不可变的
示例

我看到的唯一选择是:

  • 不要在我的生成器中使用
    潜行类型
    ,而是保存构造一个潜行类型所需的所有信息,然后在
    示例
    中构造它。虽然有效,但增加了大量的复杂性
  • 放弃
    示例
    不可变,允许构建者调用它来设置一个
    秘密
  • 揭穿鬼鬼祟祟的

  • 没有办法模仿Java版本吗?

    我看到两种可行的选择:

  • 使用
    内部

    这将使
    skillyType
    仅在Kotlin编译模块中可见

  • 使
    示例
    独立于其生成器(这是我的建议):


  • 我同意第二种选择非常好。不幸的是,对我来说,我认为它不能很好地工作,因为我实际上有一个更复杂的数据结构,它只涉及一些事情,比如
    skillyType
    。第一个选择是我最终要做的。放弃了对好的选择的投票。还没有标记“正确”,只是希望有人能想出更好的主意。谢谢
    class Example private constructor(builder: Builder) {
        private val doNotExposeThis: SneakyType
    
        init {
            doNotExposeThis = builder.doNotExposeThis
        }
    
        internal class SneakyType(x: String)
    
        class Builder {
            internal lateinit var doNotExposeThis: SneakyType
    
            fun addFoo(something: String) {
                doNotExposeThis = SneakyType(something)
            }
    
            fun build(): Example {
                return Example(this)
            }
        }
    }
    
    class Example private constructor(private val doNotExposeThis: SneakyType) {
        private class SneakyType(x: String)
    
        class Builder {
            private lateinit var doNotExposeThis: SneakyType
    
            fun addFoo(something: String) {
                doNotExposeThis = SneakyType(something)
            }
    
            fun build(): Example {
                return Example(doNotExposeThis)
            }
        }
    }