Properties and Encapsulation
Computed Property
Expose a derived value as a property.
Computed Property
ComputedProperty.kt
class Temperature(val celsius: Int) {
val fahrenheit: Int
get() {
return celsius * 9 / 5 + 32
}
}
fun main() {
val celsius =
val temperature = Temperature(celsius)
val fahrenheit = temperature.fahrenheit
println("celsius=$celsius")
println("fahrenheit=$fahrenheit")
}
class Temperature(val celsius: Int) {
val fahrenheit: Int
get() {
return celsius * 9 / 5 + 32
}
}
fun main() {
val celsius =
val temperature = Temperature(celsius)
val fahrenheit = temperature.fahrenheit
println("celsius=$celsius")
println("fahrenheit=$fahrenheit")
}
class Temperature(val celsius: Int) {
val fahrenheit: Int
get() {
return celsius * 9 / 5 + 32
}
}
fun main() {
val celsius =
val temperature = Temperature(celsius)
val fahrenheit = temperature.fahrenheit
println("celsius=$celsius")
println("fahrenheit=$fahrenheit")
}
computed
A computed property derives its value from other stored values.