能够在Kotlin中修改最终值

huangapple go评论62阅读模式
英文:

Able to modify final value in Kotlin

问题

open class OpenBase(open val value: Int)

interface AnotherExample {
    val anotherValue: OpenBase
}

open class OpenChild(value: Int) : OpenBase(value), AnotherExample {
    override var value: Int = 1000
        get() = field - 7
    override val anotherValue: OpenBase = OpenBase(value)
}

open class AnotherChild(value: Int) : OpenChild(value) {
    final override var value: Int = value
        get() = field
        set(value) { field = value * 2 }
    final override val anotherValue: OpenChild = OpenChild(value)
}

fun main() {
    val openChild = OpenChild(42)
    println("OpenChild value: ${openChild.value}")
    println("OpenChild anotherValue: ${openChild.anotherValue.value}")

    val anotherChild = AnotherChild(42)
    println("AnotherChild value: ${anotherChild.value}")
    println("AnotherChild anotherValue: ${anotherChild.anotherValue.value}")
    
    anotherChild.value=69 //HERE
    println("AnotherChild value: ${anotherChild.value}")
    println("AnotherChild anotherValue: ${anotherChild.anotherValue.value}")
}

在您的Kotlin代码中,您在AnotherChild类中使用了final关键字来覆盖了value属性。尽管value属性在其父类OpenChild中具有自定义的getter和setter方法,但您仍然可以在子类中使用final关键字覆盖它。

final关键字在Kotlin中用于防止子类进一步覆盖属性或方法。在AnotherChild类中,虽然您覆盖了value属性,但由于您使用了final关键字,它将变得不可再被子类覆盖。这意味着您可以在AnotherChild类中修改value属性的行为,但不能在后续的子类中再次覆盖它。

因此,尽管value属性在OpenChild中有自定义的getter和setter,但在AnotherChild中,您仍然可以修改它的行为,并且不会出现错误。这是因为final关键字阻止了进一步的覆盖。

英文:

open class OpenBase(open val value: Int)

interface AnotherExample {
    val anotherValue: OpenBase
}

open class OpenChild(value: Int) : OpenBase(value), AnotherExample {
    override var value: Int = 1000
        get() = field - 7
    override val anotherValue: OpenBase = OpenBase(value)
}

open class AnotherChild(value: Int) : OpenChild(value) {
    final override var value: Int = value
        get() = field
        set(value) { field = value * 2 }
    final override val anotherValue: OpenChild = OpenChild(value)
}

fun main() {
    val openChild = OpenChild(42)
    println("OpenChild value: ${openChild.value}")
    println("OpenChild anotherValue: ${openChild.anotherValue.value}")

    val anotherChild = AnotherChild(42)
    println("AnotherChild value: ${anotherChild.value}")
    println("AnotherChild anotherValue: ${anotherChild.anotherValue.value}")
    
    anotherChild.value=69 //HERE
    println("AnotherChild value: ${anotherChild.value}")
    println("AnotherChild anotherValue: ${anotherChild.anotherValue.value}")
}


I was learning kotlin OOPS and was playing with an example; I was expecting an error but got no error. Why am I able to modify the value mentioned final?

答案1

得分: 1

The final in final override var value only means that the property can't be overridden in other subclasses. If you want value to be read-only in AnotherChild, you must declare it as val instead of var. Or, if you do need a private setter, declare it as var but add private to set.

In the context of class members, val simply means "property with a getter," and var means "property with a getter and a setter." The latter is a valid override for the former, which is what you are doing in AnotherChild.

英文:

The final in final override var value only means that the property can't be overridden in other subclasses. If want value to be read only in AnotherChild, you must declare it as val instead of var. Or, if you do need a private setter, declare it as var but add private to set.

In the context of class members, val simply means "property with a getter", and var means "property with a getter and a setter. The latter is a valid override for the former, which is what you are doing in AnotherChild`.

huangapple
  • 本文由 发表于 2023年7月17日 15:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定