Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

android - Why isn't myDayForecast.map empty?

I create a object myDayForecast with secondary construction, I think the var bb will be empty because the parmater of secondary construction which pass to main construction is HashMap(). I see some document about HashMap(), it will return empty.

But after I run the code, I find the var bb isn't empty (You can see image), why?

var myDayForecast= DayForecast(15L,"Desciption",10,5,"http://www.a.com",10L)
var bb=myDayForecast.map

class DayForecast(var map: MutableMap<String, Any?>) {
    var _id: Long by map
    var date: Long by map
    var description: String by map
    var high: Int by map
    var low: Int by map
    var iconUrl: String by map
    var cityId: Long by map

    constructor(date: Long, description: String, high: Int, low: Int, iconUrl: String, cityId: Long)
            : this(HashMap()) {
        this.date = date
        this.description = description
        this.high = high
        this.low = low
        this.iconUrl = iconUrl
        this.cityId = cityId
    }
}

Result Image

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're using a map to store your property data in it. This is done via by map and works for getting and setting. By assigning a value to the properties you add elements to the map. So initially your HashMap is empty, but at the end of the constructor, you've added values to it implicitly. Kotlin doesn't create another field or mechanism to store the values otherwise.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...