Update random class attribute in Kotlin
问题 I have a class with some attributes: class DonutBox { var glaze: Int = 0 var chocolate: Int = 0 var maple: Int = 0 var etc: Int = 0 } fun addDonuts() { val omNom = DonutBox() } How can I increment a random attribute of the instantiated class? For instance, if the randomly selected attribute is chocolate , then effectively: omNom.chocolate += 1 回答1: Because Kotlin's properties are statically declared, and you want to use them dynamically, most of the methods to do that will involve reflection,