我有以下课程.
class Student(id: String, name: String) { var id: String? = null var name: String? = null var grade: String? = null constructor(id: String, name: String, grade: String) : this(id,name) { this.grade = grade } }
使用:
var student = Student("AB001","Smith","N/A") prinln(student.id + student.name + student.grade)
输出:
nullnullN/A
任何人都可以解释为什么我从默认构造函数获取输出null?
class Student { var id: String? = null var name: String? = null var grade: String? = null constructor(id: String, name: String) { this.id=id this.name=name } constructor(id: String, name: String, grade: String) : this(id, name) { this.grade = grade } }
要么
class Student(var id: String?, var name: String?) { var grade: String? = null constructor(id: String, name: String, grade: String) : this(id, name) { this.grade = grade } }