〇 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 init
structure .
The parameter declared as val
or in the main constructor var
will 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 constructor
defined 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
final
keywords 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 proactivelyfinal
declared to prohibit it from being inherited.) To make the class inheritable, in front of the class Just addopen
keywords; - 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, override
keywords 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 |
---|---|---|
|
All classes visible |
All classes are visible (default) |
|
The current class is visible |
The current class is visible |
|
The current class, subclasses, and classes under the same package path are visible |
The current class and subclasses are visible |
|
Classes in the same package path are visible (default) |
no |
|
no |
Classes in the same module are visible |
1.6 Data category:
data: When a class declaration in front of the data
keyword, 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 class
keyword to a object
keyword. In Kotlin, we do not need to privatize the constructor, nor provide getInstance()
such a static method, just class
change the object
keyword 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 immutablelist
collection (can only be used for reading, cannot add, modify or deletemutableListOf()
thelist
collection ), function is used to create a variable collection;setOf()
The function is used to create an immutableset
collection, and themutableSetOf()
function is used to create a variableset
collection;mapOf()
Function is used to create an immutablemap
collection,mutableMapOf()
function)
function is used to create a variablemap
collection;- In Kotlin, it is not recommended to use the
put()
andget()
method toMap
add 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}