赞
踩
package com.init.demo.model /** * 对象继承— 方法的覆盖 */ open class DemoPersonMethodOverride(name: String, age: Int, height: Int, likeFood: String, costByMonth: Int) { val name: String = name val age: Int = age val height: Int = height val likeFood: String = likeFood val costByMonth: Int = costByMonth /** *在方罚面前添加open,表明这个方法可以被覆盖 * 如果方法面前不添加任何修饰符,那么kotlin会默认在前面添加final * 那么方法就是不可以被继承,不可以被覆盖 */ open fun printInfomation() = println("(name='$name', age=$age, height=$height, likeFood='$likeFood', costByMonth=$costByMonth)") } class StudentMethodOverride(name: String, age: Int, height: Int, likeFood: String, costByMonth: Int,teacherNumbers :Int,schoolNmae :String):DemoPersonMethodOverride(name, age, height, likeFood, costByMonth){ var teacherNumbers :Int = teacherNumbers var schoolNmae :String = schoolNmae override fun printInfomation() { // super.printInfomation() println("(name='$name', age=$age, height=$height, likeFood='$likeFood', costByMonth=$costByMonth), teacherNumbers=$teacherNumbers), schoolNmae=$schoolNmae)") } } class WorkerMethodOverride(name: String, age: Int, height: Int, likeFood: String, costByMonth: Int,sqlary :Int,nameOfWorkPlace :String):DemoPersonMethodOverride(name, age, height, likeFood, costByMonth){ var sqlary :Int = sqlary var nameOfWorkPlace :String = nameOfWorkPlace /** * 在子类重写超类方法时,需要在方法前面加上override */ override fun printInfomation() { // super.printInfomation() println("(name='$name', age=$age, height=$height, likeFood='$likeFood', costByMonth=$costByMonth), sqlary=$sqlary), nameOfWorkPlace=$nameOfWorkPlace)") } } fun main(args: Array<String>) { val student = StudentMethodOverride("小明",20,180,"beef",800,10,"CHONGING UNIVERSITY") student.printInfomation() val worker = WorkerMethodOverride("大明",40,170,"beefLike",1600,5000,"CHONGING UNIVERSITY") worker.printInfomation() }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。