fun main(arg: Array<String>) { val map = mutableMapOf("name" to "tom", "age" to 20 ) val user = user(map) println(user.name) println(user.age) user.name="cat" println(map) map.put("age",30) println(user.age) } class user(var map:MutableMap<String,Any>){ var name:String by map var age:Int by map }
tom 20 {name=cat, age=20} 30
MutableMap委托变化是双向的,是一种可读写委托