Kotlin基本使用之接口

前面学过接口的基本使用了,本节细化了一下接口的使用方法
package net.edaibu.kotlintest.ClassAndExtends

/**
 * Created by {GEQIPENG} on 2017/5/24 at 15:42
 * 接口
 */
interface  TestInterface{
    fun getKotlin()
    fun getJava()
}
//接口类的实现
class MyClass:TestInterface {

    override fun getKotlin() {
        println("getKotlin")
    }

    override fun getJava() {
        println("getJava")
    }
}


//接口中的属性

interface  TestInterface2{
    var property1:Int
    fun foo(){
        print(property1)
    }


}

//继承包含属性的接口,一定要初始化
class MyClass2 :TestInterface2{
    override var property1: Int=100
}

//解决重写冲突

interface interfaceA{
    fun read(){
        print("read English")
    }
    fun write(){
        print(){
            print("write word")
        }
    }
}

interface interfaceB{
    fun read(){}
    fun write()
}

//注意接口中的方法如果有方法体,继承的类不用重写该方法,如果接口中的方法没有方法体,则要重写该方法
class MyClass3:interfaceB{
    override fun write() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

}
//如果类同时实现多个接口,则要重写接口中的所有方法,主意:同名方法重写一个即可
class MyClass4:interfaceA,interfaceB{
    override fun write() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun read() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值