900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > kotlin中既继承又实现_Kotlin程序| 解决继承中的主要冲突的示例

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

时间:2019-12-19 05:02:39

相关推荐

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

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 functionfun myName()// function with implementationfun sayHello(){println("Hello, 'From Interface One' ")}}interface Two{// function with implementationfun sayHello(){println("Hello, 'From Interface Two' ")}// function with implementationfun myName(){println("My Name is Interface 'Two'")}}// class implementing interfaceclass Three:One{// override interface abstract methodoverride 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 interfacesoverride fun sayHello() {// Both interface have sayHello implementation in interfaces,// so explicitly define Interface name in super to call, // specific implementation from classsuper<One>.sayHello()super<Two>.sayHello()println("Hello, From Class 'Four' ")}// need to implement all the methods // which we have inherited from multiple interfacesoverride fun myName() {// called super type implementation of method,// only interface two have implementation of this method, // so need to explicitly define interface namesuper.myName()println("My Name is Class Four")}}// Main function, Entry point of programfun main(){// create class instanceval four = Four()// call methodsfour.myName()// call methodsfour.sayHello()}

Output:

输出:

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

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

kotlin中既继承又实现

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。