当前位置:网站首页>Kotlin interface
Kotlin interface
2022-06-23 06:06:00 【Susceptible to cold】
Kotlin Interface and Java 8 similar , Use interface Keyword definition interface , Allow method to have default implementation :
interface MyInterface { fun bar() // Unrealized fun foo() { // The implemented // Optional method body println("foo") } }Implementation interface
A class or object can implement one or more interfaces .
class Child : MyInterface { override fun bar() { // Method body } }example
interface MyInterface { fun bar() fun foo() { // Optional method body println("foo") } } class Child : MyInterface { override fun bar() { // Method body println("bar") } } fun main(args: Array<String>) { val c = Child() c.foo(); c.bar(); }The output is :
foo bar
Properties in the interface
Properties in an interface can only be abstract , Initialization value... Is not allowed , The interface does not save property values , When implementing the interface , Properties must be overridden :
interface MyInterface{ var name:String //name attribute , In the abstract } class MyImpl:MyInterface{ override var name: String = "runoob" // Overridden properties }example
interface MyInterface { var name:String //name attribute , In the abstract fun bar() fun foo() { // Optional method body println("foo") } } class Child : MyInterface { override var name: String = "runoob" // Overridden properties override fun bar() { // Method body println("bar") } } fun main(args: Array<String>) { val c = Child() c.foo(); c.bar(); println(c.name) }The output is :
foo bar runoob
Function rewriting
When implementing multiple interfaces , You may encounter the problem of inheriting multiple implementations of the same method . for example :
example
interface A { fun foo() { print("A") } // The implemented fun bar() // Unrealized , There is no method body , It is abstract. } interface B { fun foo() { print("B") } // The implemented fun bar() { print("bar") } // The implemented } class C : A { override fun bar() { print("bar") } // rewrite } class D : A, B { override fun foo() { super<A>.foo() super<B>.foo() } override fun bar() { super<B>.bar() } } fun main(args: Array<String>) { val d = D() d.foo(); d.bar(); }The output is :
ABbar
Interface in the instance A and B They all define methods foo() and bar(), Both are realized foo(), B Realized bar(). because C It's an implementation A The concrete class , So we have to rewrite bar() And implement this abstract method .
However , If we're from A and B The derived D, We need to implement all methods inherited by multiple interfaces , And specify D How should they be implemented . This rule It applies to inheriting a single implementation (bar()) The method is also applicable to inheriting multiple implementations (foo()) Methods .
边栏推荐
- Pat class B 1018 C language
- Efficient office of fintech (I): automatic generation of trust plan specification
- The official artifact of station B has cracked itself!
- 【Cocos2d-x】可擦除的Layer:ErasableLayer
- Activity启动模式和生命周期实测结果
- Pyqt5 设置窗口左上角图标
- Adnroid activity截屏 保存显示到相册 View显示图片 动画消失
- 阿里云 ACK One、ACK 云原生 AI 套件新发布,解决算力时代下场景化需求
- The difference between SaaS software and traditional software delivery mode
- Alibaba cloud ack one and ACK cloud native AI suite have been newly released to meet the needs of the end of the computing era
猜你喜欢

jvm-06. Garbage collector

Real MySQL interview question (23) -- pinduoduo ball game analysis

编址和编址单位

jvm-05. garbage collection

Prometheus, incluxdb2.2 installation and flume_ Export download compile use

Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch

Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql

jvm-06.垃圾回收器

SSM project construction

最优传输理论下对抗攻击可解释性
随机推荐
Activity启动模式和生命周期实测结果
[database backup] complete the backup of MySQL database through scheduled tasks
Cryptography series: certificate format representation of PKI X.509
Wireshark TS | 视频 APP 无法播放问题
[cocos2d-x] screenshot sharing function
Matplotlib savefig multiple picture overlay
Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration
Pat class B 1017 C language
PAT 乙等 1013 C语言
Pyqt5 设置窗口左上角图标
JS面试题----防抖函数
十一、纺织面料下架功能的实现
mongodb 4.x绑定多个ip启动报错
PAT 乙等 1018 C语言
Centos7 deploy radius service -freeradius-3.0.13-15 EL7 integrating MySQL
Android handler memory leak kotlin memory leak handling
如何指定pig-register项目日志的输出路径
HierarchyViewer工具找不到 HierarchyViewer位置
Pit filling for abandoned openssl-1.0.2 (.A to.So)
机器学习3-岭回归,Lasso,变量选择技术