计算科特林的时期

时间:2020-04-20 20:05:25

标签: kotlin

为什么要这段代码:

import java.time.LocalDate
import java.time.Period

fun main(args: Array<String>) {
  val p = Person("Boris",LocalDate.of(1987,1,1))
  println("Person's age is ${p.age()}")
}

class Person(val name: String, val dateOfBirth: LocalDate = LocalDate.of(1900,1,1)) {
  fun age() {
    Period.between(this.dateOfBirth,LocalDate.now()).getYears()
  }
}

产生此输出:

Person's age is kotlin.Unit

以及如何获取打印该人的年龄(以岁为单位)?

1 个答案:

答案 0 :(得分:0)

您的年龄函数没有返回类型,因此显示单位。