gpt4 book ai didi

android - 空重载构造函数

转载 作者:行者123 更新时间:2023-11-29 14:24:13 25 4
gpt4 key购买 nike

我有以下类(class)。

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
}
}

关于android - 空重载构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34668533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com