Kotlin对象继承—次构造函数的初始化

package com.init.demo.model

/**
 * 对象继承—次构造函数的初始化
 */
open class  DemoPersonConstruction1{

    var name: String = ""
    var age: Int = 0
    var height: Int = 0
    var likeFood: String = ""
    var costByMonth: Int = 0

    constructor(name: String, age: Int, height: Int, likeFood: String, costByMonth: Int){
        this.name = name
        this.age = age
        this.height = height
        this.likeFood = likeFood
        this.costByMonth = costByMonth
    }

    fun  printInfomation() = println("(name='$name', age=$age, height=$height, likeFood='$likeFood', costByMonth=$costByMonth)")

}

/**
 *每个次构造函数不需使用super关键字对超类的属性进行初始化
 * 具体写法:constructor 子类(次构造函数参数) :super(父类次构造函数参数){}
 */
class  DemoStudentConstruction1 : DemoPersonConstruction1{

    var teacherNumbers :Int = 0
    var schoolNmae :String = ""

    constructor(name: String, age: Int, height: Int, likeFood: String, costByMonth: Int,teacherNumbers :Int,schoolNmae :String) :
            super(name, age, height, likeFood, costByMonth){
        this.teacherNumbers = teacherNumbers
        this.schoolNmae = schoolNmae
    }
}

class  DemoWorkerConstruction1 : DemoPersonConstruction1{

    var sqlary :Int = 0
    var nameOfWorkPlace :String = ""

    constructor(name: String, age: Int, height: Int, likeFood: String, costByMonth: Int,sqlary :Int,nameOfWorkPlace :String) :
            super(name, age, height, likeFood, costByMonth){
        this.sqlary = sqlary
        this.nameOfWorkPlace = nameOfWorkPlace
    }
}


fun main(args: Array<String>) {
    val student = DemoStudentConstruction1("小明",20,180,"beef",800,10,"CHONGING UNIVERSITY")
    student.printInfomation()
    val worker = DemoWorkerConstruction1("大明",40,170,"beefLike",1600,5000,"CHONGING UNIVERSITY")
    worker.printInfomation()
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值