2019.07.01 Start writing Kotlin, one watch a day, set a flag

Write a class

Members of the class

Classes can contain:
  • Constructors and initializer blocks (to summarize)
  • function
  • attribute
  • Nested and inner classes
  • Object statement
Kotlin uses the keyword class to declare classes

class HelloWorld{... } // The simplest class is OK, similar to JavaCopy the code

A class declaration consists of the class name, the class header (specifying its type parameters, main constructor, and so on), and the body of the class surrounded by curly braces. Both the class header and the class body are optional; If a class does not have a class body, you can omit the curly braces. I’m not going to show you this one.

The constructor

A class in Kotlin can have a primary constructor and one or more secondary constructors. The main constructor is part of the class header: it follows the class name (along with optional type arguments).

class HelloPeiqiTest constructor(str: String){
    //........
}Copy the code

If the main constructor does not have any annotations or visibility modifiers, omit the constructor keyword.

class HelloPeiqiTest(str: String){
    //........
}Copy the code

The main constructor cannot contain any code. Initialization code can be placed in initializer blocks prefixed with the init keyword. During instance initialization, initialization blocks are executed in the order in which they appear in the class body, interleaved with property initializers:

class HelloPeiqiTest constructor(string: String){
    var str="nihao"
    init {
        println("HelloPeiqiTest-init1")
    }
    var num:Int=21
    init {
        println("HelloPeiqiTest-init2")
    }
    init {
        println("HelloPeiqiTest-init3")}}Copy the code

The parameters of the main construct can be used in the initialization block. They can also be used in property initializers declared in the body of a class

class HelloPeiqiTest(name: String){
    var str="nihao"+name.toUpperCase()
}Copy the code



As with normal properties, properties declared in the main constructor can be mutable (var) or read-only (val).
The constructor keyword is required if the constructor has annotations or visibility modifiers and those modifiers precede it:

class HelloPeiqiTest private constructor(val name: String,val num:Int){
    init {
        println("HelloPeiqiTest-init1")}}Copy the code

subconstructor

Classes can also declare subconstructors prefixed by constructor:

class HelloPeiqiTest{
    constructor(name:String){
        println(name)
    }
    init {
        println("HelloPeiqiTest-init1")}}Copy the code

If a class has a primary constructor, each subconstructor needs to delegate to the primary constructor, either directly or indirectly through another subconstructor. To delegate to another constructor of the same class, use this:

class HelloPeiqiTest(age:Int) {
    constructor(name:String,twoAge:Int):this(twoAge){
        println(name)
    }
    init {
        println("HelloPeiqiTest-init1")}}Copy the code

Note that the code in the initialization block actually becomes part of the main constructor. The delegate to the primary constructor is the first statement of the secondary constructor, so all code in the initialization block is executed before the secondary constructor body. Even if the class does not have a primary constructor, this delegate occurs implicitly and the initialization block is still executed:

Class HelloPeiqiTest(age:Int) {constructor(name:String,twoAge:Int):this(twoAge){println(name)} init {// So println(name) println("HelloPeiqiTest-init1")}}Copy the code



If a non-abstract class does not declare any (primary or secondary) constructors, it will have a generated primary constructor with no arguments. Constructor visibility is public. If you don’t want your class to have a public constructor, you need to declare an empty main constructor with non-default visibility:

Class HelloPeiqiTest private constructor {// There is a generated primary constructor with no arguments. The default visibility is public, i.e. nothing is written. init { println("HelloPeiqiTest-init1")}}Copy the code

Note: On the JVM, if all arguments to the main constructor have default values, the compiler generates an additional no-parameter constructor that uses the default values. This makes it easier for Kotlin to use libraries like Jackson or JPA that create instances of classes through no-argument constructors.

class HelloPeiqiTest(age:Int=12) {
    init {
        println("HelloPeiqiTest-init1")}}Copy the code

Create an instance of the class
To create an instance of a class, we call the constructor just like a normal function, noting that Kotlin does not have the new keyword.

fun main(args: Array<String>) {
    println("------------start----------")
    var mHelloPeiqiTest= HelloPeiqiTest()
    println("------------stop----------")
}
class HelloPeiqiTest(age:Int=12) {
    init {
        println("HelloPeiqiTest-init-$age")}}Copy the code

Running results:

------------start----------
HelloPeiqiTest-init-12
------------stop----------Copy the code

conclusion
Class this part is mainly about how to create a class, think the initialization methods and constructors, cent gives priority to the constructor function and time, the overall writing and Java still has the same place, need to install the tutorial to write an almost can understand understand, difficulty mainly lies in The Times of the constructor to entrust the understanding of the primary constructor.