- Kotlin Standard Library Cookbook
- Samuel Urbanowicz
- 218字
- 2021-07-23 19:05:54
There's more...
It's important to keep in mind that, although we can define a default function implementation in the interface, we are not able to instantiate default values for interface properties. Unlike the class properties, properties of an interface are abstract. They don't have backing fields that could hold a current value (state). If we declare a property inside an interface, we need to implement it in the class or object that implements this interface. This is the main difference between interfaces and abstract classes. Abstract classes can have constructors and can store properties along with their implementations.
As with Java, we can't extend multiple classes; however, we can implement multiple interfaces. When we have a class implementing multiple interfaces containing default implementations, we are at risk of dealing with conflicts caused by functions having the same signatures:
interface A {
fun foo() {
// some operations
}
}
interface B {
fun foo() {
// other operations
}
}
In this case, we need to override the foo() function explicitly to resolve the conflict:
class MyClass: A, B {
override fun foo() {
print("I'm the first one here!")
}
}
Otherwise, we would get the following error:
Class 'MyClass' must override public open fun foo(): Unit because it inherits multiple interface methods of it.
- Web交互界面設計與制作(微課版)
- OpenCV 3和Qt5計算機視覺應用開發
- Android Application Development Cookbook(Second Edition)
- Cocos2d-x學習筆記:完全掌握Lua API與游戲項目開發 (未來書庫)
- Kotlin從基礎到實戰
- RESTful Java Web Services(Second Edition)
- 持續輕量級Java EE開發:編寫可測試的代碼
- Django 3.0應用開發詳解
- Visual Basic程序設計(第三版)
- Java7程序設計入門經典
- Android Studio開發實戰:從零基礎到App上線 (移動開發叢書)
- 深度實踐KVM:核心技術、管理運維、性能優化與項目實施
- MySQL數據庫應用實戰教程(慕課版)
- Python數據可視化之matplotlib實踐
- RESTful Web API Design with Node.js