Properties and Encapsulation
Computed Property
Expose a derived value as a property.
computed
A computed property derives its value from other stored values.
Computed Property
ComputedProperty.kt
Replay: real traced execution (multi-file project)
class Temperature(val celsius: Int) {
val fahrenheit: Int
get() {
return celsius * 9 / 5 + 32
}
}
fun main() {
val celsius = 20
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 = 0
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 = 30
val temperature = Temperature(celsius)
val fahrenheit = temperature.fahrenheit
println("celsius=$celsius")
println("fahrenheit=$fahrenheit")
}
celsius ← 20, fahrenheit ← 68
8fun main() {9 val celsius→ 20 = 20 //@celsius=0, 3010 val temperature = Temperature(celsius20)11 val fahrenheit→ 68 = temperature.fahrenheit681213 println("celsius=$celsius20")14 println("fahrenheit=$fahrenheit68")15}outputcelsius=20 fahrenheit=68
celsius ← 0, fahrenheit ← 32
8fun main() {9 val celsius→ 0 = 010 val temperature = Temperature(celsius0)11 val fahrenheit→ 32 = temperature.fahrenheit321213 println("celsius=$celsius0")14 println("fahrenheit=$fahrenheit32")15}outputcelsius=0 fahrenheit=32
celsius ← 30, fahrenheit ← 86
8fun main() {9 val celsius→ 30 = 3010 val temperature = Temperature(celsius30)11 val fahrenheit→ 86 = temperature.fahrenheit861213 println("celsius=$celsius30")14 println("fahrenheit=$fahrenheit86")15}outputcelsius=30 fahrenheit=86