接口和抽象类
抽象类
abstract class Person(open val age: Int){
abstract fun work()
}
class MaNong(age: Int): Person(age){
override val age: Int
get() = 0
override fun work() {
println("我是码农,我在写代码")
}
}
class Doctor(age: Int): Person(age){
override fun work() {
println("我是医生,我在给病人看病")
}
}
fun main(args: Array<String>) {
val person: Person = MaNong(23)
person.work()
println(person.age)
val person2 : Person = Doctor(24)
person2.work()
println(person2.age)
}
接口代理
class Manager: Driver, Writer {
override fun write() {
}
override fun drive() {
}
}
class SeniorManager(val driver: Driver, val writer: Writer): Driver by driver, Writer by writer
class CarDriver: Driver {
override fun drive() {
println("开车呢")
}
}
class PPTWriter: Writer {
override fun write() {
println("做PPT呢")
}
}
interface Driver{
fun drive()
}
interface Writer{
fun write()
}
fun main(args: Array<String>) {
val driver = CarDriver()
val writer = PPTWriter()
val seniorManager = SeniorManager(driver, writer)
seniorManager.drive()
seniorManager.write()
}
当前内容版权归 JackChan1999 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 JackChan1999 .