[Kotlin] interface

import java.util.*

/**
 * You can edit, run, and share this code. 
 * play.kotlinlang.org 
 */
interface Driveable {
    fun drive() 
}

interface Buildable {
    val timeRequired: Int
    fun build()
}

class Car(val color: String): Driveable {
    override fun drive() {
        println("Driving Car")
    }
}

class MotorCycle(val color: String): Driveable, Buildable {
    override val timeRequired = 120
    override fun drive() {
        println("Driving motorcycle")
    }
    
    override fun build() {
        println("Build motocycle")
    }
}


fun main() {
    val car: Driveable = Car("blue")
    car.drive()
    val mot = MotorCycle("blue")
    mot.build()
    mot.drive()
}

 

上一篇:装饰模式(也叫包装模式)


下一篇:STM32-FatFs移植进行外部flash读写操作