KOtlin type advanced

Learning objectives:

Master Kotlin type, skillfully use and advanced

Learning content:

  1. Class constructor
  2. Visibility of classes and members
  3. Delayed initialization of class properties
  4. Delegate agent
  5. Singleton object
  6. Inner class
  7. Data class
  8. Enumeration class
  9. Sealing class
  10. Inline class

Study time:

Time is like water in a sponge. There is always a squeeze

Start learning:

Section 1: class constructor

1. Definition of constructor

class Son constructor(var age :Int , var name :String){
    override fun toString(): String {
        return "$age,$name"
    }
}

The figure above defines a constructor that can omit the contractor keyword

Inside the parentheses (var age, var name) are constructor parameters and define attributes

*Removing var just defines ordinary constructor parameters

//In parentheses are constructor parameters and define class properties
class Person(var age :Int ,  name :String){
    override fun toString(): String {
        return "$age"
    }
}

In the code above, there is no var in front of name in the constructor, so name cannot be used in toString

Attributes can be defined in the constructor

//In parentheses are constructor parameters and define class properties
class Person(var age :Int ,  name :String){
    var name = name
    override fun toString(): String {
        return "$age $name"
    }
}

var age: Int is an in class global courseware

name: String visible in constructor (init block, attribute initialization)

2. What is an init block?

The init block is similar to the method body of the main constructor

Multiple init blocks can be defined and will be merged into one

Property must be initialized

class Fathor(var age :Int ,  name :String){
    var name :String
    val firstName:String

    init{
        println(name)
    }
    init {
        this.name ="123"
        println(name)
        this.firstName = name+"123"
    }
    init {
        println(firstName)
    }
}

Finally, the init block is merged into one

class Fathor(var age: Int, name: String) {
    var name: String
    val firstName: String

    init {
        println(name)
        this.name = "123"
        println(name)
        this.firstName = name + "123"
        println(firstName)
    }
}

3. Inherit parent class

//Define abstract classes
abstract class Animal

//: Animal() calls the parent constructor
class Cat(var age:Int , var name :String ): Animal()

The subclass inherits from the parent class and needs to implement the constructor of the parent class, that is, Cat needs to implement the default constructor of Animal when inheriting Animal

So: Animal ()

There are main constructors and sub constructors in constructors

//Parent constructor
class Dog(var age:Int , var name :String ): Animal() {
    //Sub constructor
    constructor(age : Int):this(age,"unkonwn"){
        //Defined inside the class after the main constructor
        //Redefined constructors are called sub constructors
        //After the sub constructor is defined, the main constructor must be called to ensure that the construction path of the class is unique
    }
}

The secondary constructor is defined inside the class of the primary constructor

Redefined constructors are called sub constructors

After the sub constructor is defined, the main constructor method must be called to ensure that the instantiation path of the class is unique

You can also not define the main constructor (not recommended)

Not defining the main constructor can cause design problems

The sub constructor needs to call the constructor super() of the parent class

The super() parent class constructor can be omitted without parameters

//Parent constructor
class Duck: Animal {
    var age :Int
    var name :String
    //Sub constructor
    constructor(age : Int , name: String){
        this.age =age
        this.name = name
    }
}

 

//Parent constructor 
//Overloaded name is given a value by default. It can be passed to name or not during instantiation
class Pig(var age:Int , var name :String  = "unkonwn"): Animal() {

}

fun main() {
    val pig = Pig(20,"pig")
    val pig2 = Pig(20)
}

* if this class wants to be invoked in the java code, it is hoped that the java code can see the overload and add annotations on the class.

@JvmOverloads

Let's start by demonstrating java calls when @ JvmOverloads is not used

Let's start by demonstrating the java call when @ JvmOverloads is used

 

 

 

Keywords: Android kotlin

Added by jvanv8 on Thu, 30 Dec 2021 14:32:43 +0200