Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package myPackage
- import kotlin.reflect.full.companionObject
- import kotlin.reflect.full.declaredMemberProperties
- class MyTestObject() {
- var name: String = "NotInitialized"
- companion object {
- val Anton = MyTestObject()
- val Berta = MyTestObject()
- val Caesar = MyTestObject()
- }
- }
- fun main(args : Array<String>) {
- println(MyTestObject.Anton.name) // name not yet initialized
- // Initialize 'name' with the variable name of the object:
- for (member in MyTestObject::class.companionObject!!.declaredMemberProperties) {
- if (member.returnType.toString() == "myPackage.MyTestObject") {
- println("$member: ${member.name}")
- // Set 'name' property of with 'member.name':
- // ???
- }
- }
- println(MyTestObject.Anton.name) // now with the initialized name
- }
Add Comment
Please, Sign In to add comment