5.object

object关键字,常用来作为kotlin中的单例模式
定义:
1.只有一个实例的类
2.不能自定义构造方法
3.可以实现接口,继承父类
4.反编译出来的class文件,就是java中static代码块中new对象的单例模式

class Driver10
interface OnExternalDriverMountListener{
    fun onMount(driver: Driver10)
    fun onUnmount(driver: Driver10)
}
abstract class Player
object MusicPlayer: Player(), OnExternalDriverMountListener{
    override fun onMount(driver: Driver10) {}
    override fun onUnmount(driver: Driver10) {}
    val state : Int = 0
    fun play(url : String){}
    fun stop(){}
}

fun main(args: Array) {
    MusicPlayer.play("")
    println(MusicPlayer.state)
}

你可能感兴趣的:(5.object)