kotlin之MutableMap委托

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委托变化是双向的,是一种可读写委托

posted on 2018-12-19 15:11  LoaderMan  阅读(1436)  评论(0编辑  收藏  举报

导航