美文网首页
14. 接口

14. 接口

作者: 努力生活的西鱼 | 来源:发表于2019-11-23 15:35 被阅读0次
Kotlin
  • Kotlin的接口可以既包含抽象方法的声明也包含实现。
  • 使用关键字interface来定义接口。
  • 一个类或者对象可以实现一个或者多个接口。
// 定义接口
interface InputDevice {

    fun input(event: Any);

}

// 一个接口继承一个接口
interface USBInputDevice : InputDevice

interface BLEInputDevice : InputDevice

interface OpticalMouse

// 一个类实现多个接口
class USBMouse(var name:String):USBInputDevice,OpticalMouse {
    override fun input(event: Any) {

    }

    override fun toString(): String {
        return name;
    }

}

class Computer {

    fun addUSBInputDevice(inputDevice: USBInputDevice) {
        println("add usb input device: $inputDevice");
    }

    fun addBLEInputDevice(inputDevice: BLEInputDevice) {
        println("add BLE input device: $inputDevice");
    }

    fun addInputDevice(inputDevice: InputDevice) {
        when (inputDevice) {
            is USBInputDevice -> {
                addUSBInputDevice(inputDevice)
            }
            is BLEInputDevice -> {
                addBLEInputDevice(inputDevice)
            }
            else -> {
                throw IllegalAccessException("输入设备类型不支持")
            }
        }
    }
}

fun main() {
    val computer = Computer();
    val mouse = USBMouse("罗技的鼠标");
    computer.addInputDevice(mouse);
}
接口中的属性

可以在接口中定义属性。在接口中声明的属性,要么是抽象的,要么提供访问器实现。在接口中声明的属性不能有幕后字段,因此接口中声明的访问器不能引用它们。

接口与抽象类的区别
  • 抽象类有状态,接口没有状态(属性)
  • 抽象类有方法实现,接口只能有无状态的默认实现
  • 抽象类只能单继承,接口可以多实现
  • 抽象类反应本质,接口体现能力
abstract class A {

    var i = 0;

    fun hello() {
        println(i);
    }

    open fun hello1() {
        println(i);
    };

    abstract fun hello2();
}

interface B {
    var j: Int;

    // 有默认实现
    fun hello3() {
        println(j);
    }

    fun hello4();
}

interface C;

class D(override var j: Int) : B {

    // 需要重写
    override fun hello4() {

    }

}

class E : A() {

    // 可以重写父类的方法
    override fun hello1() {

    }

    // 有abstract,则必须重写与接口中的类似
    override fun hello2() {

    }

}

class F(override var j: Int) : A(), B, C {
    override fun hello2() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

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

}

相关文章

  • 14.接口

    实现防盗门的功能 第一种方案 门有“开”和“关”的功能,锁有“上锁”和“开锁”的功能 将门和锁分别定义为抽象类 防...

  • 14.接口

    接口及频率

  • 14. 接口

    Kotlin的接口可以既包含抽象方法的声明也包含实现。 使用关键字interface来定义接口。 一个类或者对象可...

  • 14. 接口定义

    接口定义 可能对于小白来说,最难理解的是什么是接口,这个也确实比较抽象。但其实在requests库使用那一章节已经...

  • 14.接口与API设计

    1、使用前缀避免命名空间冲突 选择与你、你公司、应用程序或与之皆关联之名作为类名的前缀,并在所有代码(类名, 方法...

  • Java面试题--持续更新4

    14.接口和抽象类的区别是什么? 从设计层面来说,抽象是对类的抽象,是一种模板设计,接口是行为的抽象,是一种行为的...

  • 浅谈Solidity: 14. 抽象合约和接口

    我们用ERC721的接口合约为例介绍solidity中的抽象合约(abstract)和接口(interface),...

  • LeetCode 14. Longest Common Pref

    14. Longest Common Prefix

  • LeetCode 14. Longest Common Pref

    14. Longest Common Prefix c++代码:

  • Stray Birds

    14. The mystery of creation is like the darkness of night...

网友评论

      本文标题:14. 接口

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