【发布时间】:2018-02-10 05:32:24
【问题描述】:
我正在为Kotlin 的gigasecond 练习练习寻找解决方案:http://exercism.io/exercises/kotlin/gigasecond/readme。我可以理解它如何需要两个两个构造函数,因为在创建类时会传入 LocalDate 和 LocalDateTime 参数。我不明白的是下面的二级类构造函数变量是如何传入并在类中使用的。似乎只有在传入LocalDateTime 参数时才会进行计算,因为只有dobWithTime 才能完成计算。这里发生了什么魔法?
data class Gigasecond(val dobWithTime: LocalDateTime) {
constructor(dateOfBirth: LocalDate) : this(dateOfBirth.atStartOfDay())
val date: LocalDateTime = dobWithTime.plusSeconds(1000000000)
}
【问题讨论】:
标签: constructor kotlin