cool hit counter Kotlin syntax - modifiers, extensions_Intefrankly

Kotlin syntax - modifiers, extensions


This series of articles to learn about Kotlin and Anko plugins Develop an Android project through Kotlin.

Kotlin-Anko Learning(1) Kotlin, Anko Introduction

Kotlin-Anko Learning (2) Kotlin Syntax Basics - Basic Types

Kotlin-Anko Learning (3) Kotlin Syntax Basics - Keyword package, Import, if, when, for, while, return, break, continue

Kotlin-Anko Learning (4) Kotlin Syntax - Classes, Inheritance, Abstract Classes

Kotlin-Anko Learning (5) Kotlin Syntax - Properties, Fields, Interfaces

Kotlin-Anko Learning (6) Kotlin Syntax - Modifiers

modifier

There are these four visibility modifiers in Kotlin: private, protected, internal, and public. The default visibility is public if the modifier is not explicitly specified. where the visibility of the internal modifier is the same module, which is not available in java.

Modifiers for modifiers: classes, objects, interfaces, constructors, methods, properties and their setters can all have visibility modifiers. (The getter always has the same visibility as the property. )

modular concept

A module in Kotlin refers to a set of Kotlin files compiled together: the

An IntelliJ IDEA module

A Maven project

A Gradle source set

A set of files compiled by one <kotlinc> Ant task execution

Top level statement

(located) atKotlin in, Functions can be declared directly under the package、 Properties and Classes、 Objects and interfaces, They are called Top level statement, In particular, the top-level function、 Top-level properties、 top-level object bejava Not allowed in。

Rules for the use of modifiers in top-level declarations.

Do not specify any visibility modifier, the default is, which means your declaration will be visible everywhere

If you declare it as , it will only be visible within the file that declares it

If you declare it as , it will be visible everywhere within the same module

Not applicable to top-level statements

General statement

Member declarations inside classes and interfaces in Kotlin can be visibility-modified just like java

Rules for the use of modifiers in classes.

private - visible within this class (containing all its members).

protected - same as private + visible in subclasses.

internal -- any client within this module that can see the class declaration has its internal members visible.

public -- the public members are visible to any client that can see the class declaration

Internal classes - external classes cannot access private members of internal classes (unlike java)

Derived Classes - A subclass overrides a protected member of a parent class, and if visibility is not specified, then it is also protected in the subclass.

Here the basic rules for the use of visibility are covered. Next learn the concept of extension

extensions

extensions As the name implies, it adds code to the extensions sex,java in which we can inherit from、 Decorator pattern to add functionality to classes,(located) atKotlin A special declaration can be passed in, functional extensions,Kotlin there is in it Extended functions harmony Extended Properties。

Extended functions

Kotlin in, Extended functions spelling:" fun Receiver Type. Function Name() " That is, being extensions type to be used as the Extended functions prefix。

extensions is statically resolved: extensions method does not add a new member to the class, Just by"." expression to call this new function( i.e.: called Extended functions is determined by the type of the expression in which the function call is made, Instead, it is determined by the result of the expression's runtime evaluation)

Output results.

Extended functions Can have the same name as a member function of a class, Priority calls to member functions, If it is an overload of a member function, can be called。

Output results.

Extension functions can define nullable (null) receivers, i.e., extension functions of the null receiver type that support nullable types without changing the functions in the class, with the determination of the null type being inside the extension function.

Output results.

As you can see from the above, in caseD Type is empty, invoke d.doo() method will report an error, We can define nullable doo() Extended functions settle (a dispute)。Any.toString() Just use this method to solve the problem of accepting nullable types。

Extended Properties

Extended Properties together with Extended functions use the same, No actual insertion of members into the class, So for the behind-the-scenes fields that are not valid, i.e.: Extended Properties Can't have an initializer, Only the displayedget、set approach。

Companion object extension

If a companion object is defined in the class, we can extend the functions and properties of the companion object.

Extended scopes

For top-level extensions, in other packages, you can call the

Extended declaration as a member

Inside a class you can declare extensions for another class. If there are multiple implicit receivers - the object members of which can be accessed without qualifiers. The instance of the class in which the extension declaration is located is called the distribution receiver, and the instance of the receiver type in which the extension method call is located is called the extension receiver. In the case of a conflict between the member names of a distribution recipient and an extension recipient, the extension recipient takes precedence. Access can also be specified by qualifiers

Output results.

motivations

The motivation is understood as optimizing the way the code is written and writing a more worrying code by extension.

For example, the Collections tool class in java.

Here we finish introducing the concepts related to extensions, the inclusion of extensions makes it possible to better handle the extensibility of the code in solving everyday problems.

consult

Official website: https://www.kotlincn.net


Recommended>>
1、Chinas first superhighway to open in 2022 without fear of speeding
2、Blockchain New Economy Hangzhou Summit 2018 Ends Perfectly Hangzhou Wants to Build Chinas Blockchain Capital
3、Verints Smart HRM Suite Boosts Workforce Efficiency with AI Technology
4、What did these blockchain tech gurus talk about in 2018
5、How to learn C language systematically No more confused time wasters

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号