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.
}
}
前面学过接口的基本使用了,本节细化了一下接口的使用方法package net.edaibu.kotlintest.ClassAndExtends/** * Created by {GEQIPENG} on 2017/5/24 at 15:42 * 接口 */interface TestInterface{ fun getKotlin() fun getJava()}