为什么myDayForecast.map不为空? [英] Why isn't myDayForecast.map empty?
问题描述
我用辅助构造创建了一个对象myDayForecast,我认为var bb将为空,因为传递给主构造的辅助构造的参数是HashMap(). 我看到一些有关HashMap()的文档,它将返回空.
但是在运行代码之后,我发现var bb不是空的(您可以看到图像),为什么?
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
}
}
结果图片
如果辅助构造函数定义行在末尾,请输入:
constructor(date: Long, description: String, high: Int, low: Int, iconUrl: String, cityId: Long)
: this(HashMap()) {
: this(HashMap())
是对主要构造函数的调用:
DayForecast(var map: MutableMap<String, Any?>)
但使用一个空的HashMap作为参数.
因此,当您拨打电话时:
var myDayForecast=DayForecast(15L,"Desciption",10,5,"http://www.a.com",10L)
首先发生的事情之一是使用上述空HashMap调用主构造函数.
这就像您这样调用主构造函数:
DayForcast(map = HashMap())
因此现在地图已设置为空的HashMap.
在辅助构造函数中,每个字段都用by
map
标记,其中map
是DayForecast的MutableMap属性.如此处所示:
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
...
}
这意味着对这些字段的任何访问都委托给map
所引用的对象,在这种情况下,该对象是MutableMap
对象.对于MutableMap对象,这意味着编译器会将诸如this.date = 15L
之类的调用转换为诸如this.map.put("date",15L)之类的内容,而将诸如blah = this.date
之类的引用转换为诸如blah = this.map.get("date")
<之类的内容. /p>
接下来,在调用主构造函数之后,将运行辅助构造函数的第二部分.
this.date = date
this.description = description
this.high = high
this.low = low
this.iconUrl = iconUrl
this.cityId = cityId
现在,因为这些属性中的每一个都被声明为var propXYZ by map
每个调用都转换为this.map.put("date", date)
之类的调用,这些调用将使用值填充最初为空的HashMap,以便在您调用
var bb=myDayForecast.map
,地图现在已填充在HashMap中.
请查看Kotlin的代理属性部分文档,如果仍然令人困惑的话.
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
In at the end if the secondary constructor definition line:
constructor(date: Long, description: String, high: Int, low: Int, iconUrl: String, cityId: Long)
: this(HashMap()) {
: this(HashMap())
is a call to the primary constructor:
DayForecast(var map: MutableMap<String, Any?>)
but with an empty HashMap as the argument.
So when you make the call :
var myDayForecast=DayForecast(15L,"Desciption",10,5,"http://www.a.com",10L)
one of the first things that happens is that the primary constructor is called with the empty HashMap as noted above.
This is as if you called the primary constructor like this:
DayForcast(map = HashMap())
So now map has been set to an empty HashMap.
In the secondary constructor, each of the fields are marked with by
map
, where map
is a MutableMap property of DayForecast. as seen here:
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
...
}
This means that any access to those fields is delegated to the object referred to by map
, which in this case is a MutableMap
object. For a MutableMap object this means that the compiler will translate calls such as this.date = 15L
into something like this this.map.put("date", 15L) and references like blah = this.date
will be translated into something like, blah = this.map.get("date")
Next, after the primary constructor has been called, the second part of the secondary constructor is run.
this.date = date
this.description = description
this.high = high
this.low = low
this.iconUrl = iconUrl
this.cityId = cityId
Now since each of these properties are declared as var propXYZ by map
each of these calls are translated into calls like this.map.put("date", date)
, which will fill in the initially empty HashMap with values, so that by the time you call
var bb=myDayForecast.map
, map is now a filled in HashMap.
Please take a look at the delegate-properties section of the Kotlin documentation, if this is still confusing.
这篇关于为什么myDayForecast.map不为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!