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 interface
interface 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 interface
class Three:One{
// override interface abstract method
override fun myName() {
println("My Name is Class Three")
}
}
// class implementing more then one interfaces
class 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<One>.sayHello()
super<Two>.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 program
fun 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 Four
Hello, 'From Interface One'
Hello, 'From Interface Two'
Hello, From Class 'Four'

翻译自: https://www.includehelp.com/kotlin/example-of-resolving-overriding-conflicts-in-inheritance.aspx

kotlin中既继承又实现

kotlin中既继承又实现_Kotlin程序| 解决继承中的主要冲突的示例相关推荐

  1. kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符

    kotlin中判断字符串 Given a string and a character, we have to remove all occurrences of the character in g ...

  2. 无法启动此程序因为计算机中丢失msvcr110,无法启动此程序因为计算机中丢失msvcr110,教您无法运行程序提示计算机中丢失...

    用户在一次运行某程序时,运到"无法启动此程序,因为计算机中丢失msvcr.dll.尝试重新安装该程序以解决此问题."的提示,当时很无语,因为系统是刚刚安装好的,怎么处理呢?下面,小 ...

  3. 无法启动此程序因为计算机中丢失msvcr110,无法启动此程序因为计算机中丢失msvcr110,详细教您无法运行程序提示计算机中丢失msvcr110怎么解决...

    在计算机的使用过程中我们经常会遇见各种.dll丢失.缺少.找不到等问题出错问题.遇见这种情况不要着急,将丢失的dll文件记录下来,然后寻找解决方法即可.那么遇到无法运行程序提示计算机中丢失msvcr1 ...

  4. python中plot不能显示标签_python 2: 解决python中的plot函数的图例legend不能显示中文问题...

    问题: 图像标题.横纵坐标轴的标签都能显示中文名字,但是图例就是不能显示中文,怎么解决呢? 解决: plt.figure() plt.title(u'训练性能', fontproperties=fon ...

  5. kotlin中判断字符串_Kotlin程序查找字符串中字符的频率

    kotlin中判断字符串 Given a string and a character, we have to find the frequency of the character in the s ...

  6. kotlin 字符串去空格_Kotlin程序从字符串中删除所有空格

    kotlin 字符串去空格 Given a string, we have to remove all whitespaces from it. 给定一个字符串,我们必须从中删除所有空格. Examp ...

  7. java代码中哪些不能犯的错误_Java程序员工作中千万不能犯的3个低级错误

    常在河边走,哪有不湿鞋.Java工程师也是人,他们也会犯错误.但是千锋广州小编提醒大家有三个错误是万万不能犯的!工作中犯了这三个错误,今年的加薪就和你say goodbye了!快看看自己是不是中枪了. ...

  8. c语言程序解决生活中的问题作文,生活中烦恼的事五年级满分作文

    在我们人生的道路上,总会遇到这样那样的烦恼,困难与挫折,但在那时候,我们更不能惊慌,要静下心来沉住气,认真的去想解决的方法.勇敢地去面对,这是人生的一道坎儿.下面是小编为大家整理的关于生活中烦恼的事五 ...

  9. c语言在中职的作用,C语言程序下的中职教学论文

    一.树立学生良好的学习信心 在日常的中职计算机教育教学中,应当重视学生对教学内容的看法,大多数学生认为C语言学习相对较为困难,在日常的中职计算机学习中,学生某种程度上缺乏应有的信心以及勇气,而尽管有的 ...

最新文章

  1. Visual C++ 2010中更换MFC对话框默认图标
  2. python壁纸程序代码_python设置windows桌面壁纸的实现代码
  3. canvas写的一个刮奖效果
  4. 生成JSON数据--fastjson(阿里)方法
  5. 5页面如何切图_如何让你的设计稿做到95%还原?
  6. 【Usaco2014Open银组】照相(pairphoto)
  7. Flink 1.10 细粒度资源管理解析
  8. ios安装python的步骤_如何利用 Python 爬虫实现给微信群发新闻早报?(详细)
  9. 【youcans 的 OpenCV 例程200篇】147. 图像分割之孤立点检测
  10. 两张照片重叠处半透明_那些悬浮在空中的照片是怎么拍的?
  11. 属猴的人2021年运势预测
  12. JAVA开发的人力资源管理系统
  13. 《论文写作》课堂总结
  14. 王者荣耀国际服——传说对决
  15. 分治法 divide and conquer
  16. waiting for changelog lock.
  17. 打算的亲爱额请问请问额
  18. 实时最新中国省市区县geoJSON格式地图行政边界数据Echarts地图数据(可精确到街道级)
  19. 35岁,成就寥寥,为时已晚?(I am 35 and I haven't achieved much in life. Is it too late?)...
  20. 【协作MIMO+非规则LDPC】协作MIMO系统上,中继协作解码转发策略和编码协作策略,采用非规则LDPC编码

热门文章

  1. 平板电脑有什么用_除了盖泡面,平板电脑没什么用了
  2. 如何把照片正面变成反面_各国签证照片要求大全 (含模板)
  3. JDK源码解析之 Java.lang.Enum
  4. vscode Go 1.11.4 编译错误 need Delve built by Go 1.11 or later
  5. 分布式数据库中间件使用经验分享
  6. POJ1061:青蛙的约会——题解
  7. 自定义控件---重写Listbox实现item图标变换和item点击事件
  8. pycharm下载与安装
  9. ApacheHttpServer修改httpd.conf配置文件
  10. 浅谈Jfinal急速开发框架