Guest User

Untitled

a guest
Oct 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. package myPackage
  2.  
  3. import kotlin.reflect.full.companionObject
  4. import kotlin.reflect.full.declaredMemberProperties
  5.  
  6. class MyTestObject() {
  7.  
  8. var name: String = "NotInitialized"
  9.  
  10. companion object {
  11. val Anton = MyTestObject()
  12. val Berta = MyTestObject()
  13. val Caesar = MyTestObject()
  14. }
  15. }
  16.  
  17. fun main(args : Array<String>) {
  18. println(MyTestObject.Anton.name) // name not yet initialized
  19.  
  20. // Initialize 'name' with the variable name of the object:
  21. for (member in MyTestObject::class.companionObject!!.declaredMemberProperties) {
  22. if (member.returnType.toString() == "myPackage.MyTestObject") {
  23. println("$member: ${member.name}")
  24.  
  25. // Set 'name' property of with 'member.name':
  26. // ???
  27. }
  28. }
  29.  
  30. println(MyTestObject.Anton.name) // now with the initialized name
  31. }
Add Comment
Please, Sign In to add comment