"public read-only" 访问修饰符?

"public read-only" access modifier?

"traditional" 实施:

interface IFoo{
    fun getS():String
    fun modifyS():Unit
}

class Foo : IFoo{
    private var s = "bar"

    override fun getS() = s.toUpperCase()
    override fun modifyS(){ s = when(s){
        "bar" -> "baz"
        else -> "bar"
    }}
}

我现在想要的是这样的:

interface IFoo{
    var s:String
        protected set

    fun modifyS():Unit
}

class Foo : IFoo{
    override var s = "bar"
        protected set
        get() = field.toUpperCase()

    override fun modifyS(){ s = when(s){
        "bar" -> "baz"
        else -> "bar"
    }}
}

我有预感答案是否定的,但是......

有什么办法可以做到这一点?

无法将接口成员的可见性限制为 protected

但是,您可以在接口中定义 val 并在实现中定义 override it with a var

interface IFoo {
    val s: String
}

class Foo : IFoo {
    override var s = "bar"
        protected set
        get() = field.toUpperCase()
}