Learning objectives:
Master Kotlin type, skillfully use and advanced
Learning content:
- Class constructor
- Visibility of classes and members
- Delayed initialization of class properties
- Delegate agent
- Singleton object
- Inner class
- Data class
- Enumeration class
- Sealing class
- 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