测试 kotlin
import kotlinx.serialization.Serializable
import kotlin.random.Random
interface Building
@Serializable
class House(
private val rooms: Int? = 3,
val name: String = "Palace"
) : Building {
var residents: Int = 4
get() {
println("Current residents: $field")
return field
}
fun burn(evacuation: (people: Int) -> Boolean) {
rooms ?: return
if (evacuation((0..residents).random()))
residents = 0
}
}
fun main() {
val house = House(name = "Skyscraper 1")
house.burn {
Random.nextBoolean()
}
}

浙公网安备 33010602011771号