博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kotlin中既继承又实现_Kotlin程序| 解决继承中的主要冲突的示例
阅读量:2527 次
发布时间:2019-05-11

本文共 2122 字,大约阅读时间需要 7 分钟。

kotlin中既继承又实现

继承中的主要冲突 (Overriding Conflicts in Inheritance)

  • It may appear, we inherit more than one implementation of the same method.

    看来,我们继承了同一方法的多个实现。

  • Need to implement all the methods which we have inherited from multiple interfaces.

    需要实现我们从多个接口继承的所有方法。

解决Kotlin中继承中的主要冲突 (Resolving Overriding Conflicts in Inheritance in Kotlin)

package com.includehelp// declare interfaceinterface One{    // abstract function    fun myName()    // function with implementation    fun sayHello(){        println("Hello, 'From Interface One' ")    }}interface Two{    // function with implementation    fun sayHello(){        println("Hello, 'From Interface Two' ")    }    // function with implementation    fun myName(){        println("My Name is  Interface 'Two'")    }}// class implementing interfaceclass Three:One{    // override interface abstract method    override fun myName() {        println("My Name is Class Three")    }}// class implementing more then one interfacesclass Four:One,Two{    // need to implement all the methods     // which we have inherited from multiple interfaces    override fun sayHello() {        // Both interface have sayHello implementation in interfaces,        // so explicitly define Interface name in super to call,         // specific implementation from class        super
.sayHello() super
.sayHello() println("Hello, From Class 'Four' ") } // need to implement all the methods // which we have inherited from multiple interfaces override fun myName() { // called super type implementation of method, // only interface two have implementation of this method, // so need to explicitly define interface name super.myName() println("My Name is Class Four") }}// Main function, Entry point of programfun main(){ // create class instance val four = Four() // call methods four.myName() // call methods four.sayHello()}

Output:

输出:

My Name is  Interface 'Two'My Name is Class FourHello, 'From Interface One' Hello, 'From Interface Two' Hello, From Class 'Four'

翻译自:

kotlin中既继承又实现

转载地址:http://yjtzd.baihongyu.com/

你可能感兴趣的文章
jquery扩展 $.fn
查看>>
tomcat 多实例的Sys V风格脚本
查看>>
程序员如何讲清楚技术方案
查看>>
MapReduce-实践1
查看>>
UVa 815 - Flooded!
查看>>
jQuery基础--选择器
查看>>
mybatis使用collection查询集合属性规则
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
P2731 骑马修栅栏 欧拉函数
查看>>
sort函数
查看>>