美文网首页
抽象类和接口

抽象类和接口

作者: GaoXiaoGao | 来源:发表于2020-12-18 09:07 被阅读0次

    接口:代表的是事物的能力
    抽象类:反映的是事物的本质

    kotlin中的接口

    interface IMan {
        fun xiao()
    }
    

    kotlin中的抽象类

    abstract class Human {
        abstract fun eat()
    }
    

    kotlin中实现接口

    class Man :IMan{
        override fun xiao() {
            println("哈哈")
        }
    }
    
    

    kotlin中实现继承抽象类

    class Man :Human(){
    
        override fun eat() {
            println("我会吃饭")
        }
    }
    
    

    两者实现的区别是: 实现接口时 : 后面直接跟接口类名称
    继承抽象类时 : 后面需要添加()

    同时继承抽象类和接口

    class Man :Human(),IMan{
        override fun xiao() {
            println("哈哈")
        }
    
        override fun eat() {
            println("吃饭")
        }
    }
    

    相关文章

      网友评论

          本文标题:抽象类和接口

          本文链接:https://www.haomeiwen.com/subject/vydzgktx.html