Classes and Objects
Object State
Use a value stored inside an object.
state
Object state is data stored by an object and used by its methods.
Object State
ObjectState.kt
Replay: real traced execution (multi-file project)
class ScoreBox(var score: Int) {
fun afterAdd(points: Int): Int {
return score + points
}
}
fun main() {
val points = 4
val box = ScoreBox(10)
val finalScore = box.afterAdd(points)
println("points=$points")
println("score=$finalScore")
}
class ScoreBox(var score: Int) {
fun afterAdd(points: Int): Int {
return score + points
}
}
fun main() {
val points = 1
val box = ScoreBox(10)
val finalScore = box.afterAdd(points)
println("points=$points")
println("score=$finalScore")
}
class ScoreBox(var score: Int) {
fun afterAdd(points: Int): Int {
return score + points
}
}
fun main() {
val points = 6
val box = ScoreBox(10)
val finalScore = box.afterAdd(points)
println("points=$points")
println("score=$finalScore")
}
points ← 4
7fun main() {8 val points→ 4 = 4 //@points=1, 69 val box = ScoreBox(10)10 val finalScore = box.afterAdd(points4)fun afterAdd(points: Int): Int
1class ScoreBox(var score: Int) {2 fun afterAdd(points4: Int): Int {3 return score10 + points44 }finalScore ← 14
9 val box = ScoreBox(10)10 val finalScore→ 14 = box.afterAdd(points4)1112 println("points=$points4")13 println("score=$finalScore14")14}outputpoints=4 score=14
points ← 1
7fun main() {8 val points→ 1 = 19 val box = ScoreBox(10)10 val finalScore = box.afterAdd(points1)fun afterAdd(points: Int): Int
1class ScoreBox(var score: Int) {2 fun afterAdd(points1: Int): Int {3 return score10 + points14 }finalScore ← 11
9 val box = ScoreBox(10)10 val finalScore→ 11 = box.afterAdd(points1)1112 println("points=$points1")13 println("score=$finalScore11")14}outputpoints=1 score=11
points ← 6
7fun main() {8 val points→ 6 = 69 val box = ScoreBox(10)10 val finalScore = box.afterAdd(points6)fun afterAdd(points: Int): Int
1class ScoreBox(var score: Int) {2 fun afterAdd(points6: Int): Int {3 return score10 + points64 }finalScore ← 16
9 val box = ScoreBox(10)10 val finalScore→ 16 = box.afterAdd(points6)1112 println("points=$points6")13 println("score=$finalScore16")14}outputpoints=6 score=16