Delegation Kotlin Android Studio Tutorial

61 Просмотры
Издатель
Delegation controls the allocation of power/authority from an instance to another for any object. For classes and functions implementations, delegations can be used on static as well as mutable relations between them. Inheritance implementation in classes and functions can be altered with the help of delegation techniques and object-oriented programing languages support it innately without any boilerplate code. Delegation is used in Kotlin with the help of “by” keyword.

There are two types of delegation present in Kotlin:
• Explicit delegation: Supported by all object-oriented language and it is done by passing a delegate(the one to be implemented) object to delegating object (the one that will implement delegate object).
• Implicit delegation: Requires language-level support for the delegation pattern.

The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.
The by-clause in the supertype list for Derived indicates that b will be stored internally in objects of Derived and the compiler will generate all the methods of Base that forward to b.

#kotlin #delegation #androidappdevelopment #jetpackcompose
Категория
Язык программирования Kotlin
Комментариев нет.