当前位置:  开发笔记 > 编程语言 > 正文

空重载构造函数

如何解决《空重载构造函数》经验,为你挑选了1个好方法。

我有以下课程.

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?



1> 小智..:
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
    }
}

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有