Kotlin language study notes

〇 Preface

At the 2017 Google Developers Conference, Google introduced the new Android development language Kotlin. At the 2019 Developers Conference, Google went one step further and announced Kotlin Frist. With the promotion of Google, more and more Android development has been switched from Java to Kotlin in recent years, and some famous third-party libraries are also being rewritten in Kotlin. So it's time to summarize Kotlin's study notes, in order to review the past and learn the new. My learning route is mainly based on the "third line of code", so the notes are also based on the "third line of code".

One, object-oriented

1.2 Constructor:

Constructor: Any object-oriented programming language will have the concept of constructor, and there is also in Kotlin, but Kotlin divides constructors into two types: primary constructor and secondary constructor.

Main constructor: The main constructor is characterized by no function body, which can be defined directly after the class name. By default, each class will have a main constructor without parameters, of course, you can also specify the parameters to it explicitly. The main constructor will be our most commonly used constructor.

In addition: all the logic in the main constructor can be written in the initstructure .

The parameter declared as valor in the main constructor varwill automatically become the field of the class. If the parameter in the main constructor is not preceded by any keywords, its scope is limited to the main constructor.

Sub-constructor: The sub-constructor is constructordefined by keywords and has a function body. Any one class can only have one primary constructor, but there can be multiple secondary constructors. The secondary constructor can also be used to instantiate a class, which is no different from the primary constructor.

Kotlin stipulates that when a class has both a primary constructor and a secondary constructor, all secondary constructors must call the primary constructor (including indirect calls).

Kotlin allows classes to have only secondary constructors and no primary constructors. When a class does not explicitly define a primary constructor and defines a secondary constructor, it does not have a primary constructor.

1.3 Inheritance:

  • In Kotlin, any non-abstract class cannot be inherited by default, which is equivalent to declaring finalkeywords for the class in Java . ( It is clearly mentioned in the book Effective Java that if a class is not specifically designed for inheritance, then it should be proactively finaldeclared to prohibit it from being inherited.) To make the class inheritable, in front of the class Just add openkeywords;
  • Inheritance in Java keywords are extends, and become a colon in Kotlin in ( : );
  • In the Java inheritance feature, the constructor of the subclass must call the constructor of the parent class, and this rule must also be followed in Kotlin. In Kotlin, the main constructor of the subclass calls which constructor in the parent class, which is specified by parentheses when inheriting. Even if there are no parameters, the pair of parentheses cannot be omitted;

1.4 Interface:

Java is a language with a single-inheritance structure. Any class can only inherit from one parent class at most, but it can implement any number of interfaces. Kotlin is the same;

The key word inherited in Java is extends, the key word used to implement the interface is implements, while in Kotlin, the colon is used uniformly, and the middle is separated by a comma. In addition, there is no need to add parentheses after the interface, because it has no constructor to call;

In Kotlin, overridekeywords are used to rewrite parent classes or implement functions in interfaces;

Like JDK 1.8, Kotlin allows default implementation of functions defined in the interface. If a function in the interface has a function body, the content of this function body is its default implementation. The default implementation function subclass can freely choose to implement or not, and the default implementation logic will be automatically used when it is not implemented.

1.5 Function visibility modifier

Java and Kotlin function visibility modifier comparison table

Modifier

Java

Kotlin

public

All classes visible

All classes are visible (default)

private

The current class is visible

The current class is visible

protected

The current class, subclasses, and classes under the same package path are visible

The current class and subclasses are visible

default

Classes in the same package path are visible (default)

no

internal

no

Classes in the same module are visible

1.6 Data category:

data: When a class declaration in front of the datakeyword, to show that you want this data class is a class, will help Kotlin The primary constructor parameters you will equals(), hashCode(), toString()or the like generated automatically.

1.7 Singleton class:

Although the singleton implementation in Java is not complicated, Kotlin does a better job. It hides some fixed and repetitive logic implementations and only exposes us to the simplest and most convenient usage. The way to create a singleton class in Kotlin is extremely simple, just change the classkeyword to a objectkeyword. In Kotlin, we do not need to privatize the constructor, nor provide getInstance()such a static method, just classchange the objectkeyword to a keyword, and a singleton class is created.

Two, Lambda programming

Lambda in Kotlin is extremely powerful

2.1 The creation of the collection:

  • listOf()Function is used to create an immutable listcollection (can only be used for reading, cannot add, modify or delete mutableListOf()the listcollection ), function is used to create a variable collection;
  • setOf()The function is used to create an immutable setcollection, and the mutableSetOf()function is used to create a variable setcollection;
  • mapOf()Function is used to create an immutable mapcollection, mutableMapOf()function )function is used to create a variable mapcollection;
  • In Kotlin, it is not recommended to use the put()and get()method to Mapadd and read data operations, but it is more recommended to use a syntax structure similar to array subscripts;

2.2 Collection of functional API:

Definition of Lambda: Lambda is a small piece of code that can be passed as a parameter.

The grammatical structure of Lambda expression: {parameter name 1: parameter type, parameter name 2: parameter type -> function body}

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/beita08/article/details/113731887