Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

👉 About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

👉 premise

The current environment

2020.3.1 Patch 2 version was last downloaded on October 8, 2021

👉 Practice

😜 Classes and objects

Classes are also created using the class keyword

class Entity {
     var name: String? = null
     var urlJUEJIIN: String? = null
     var urlCSDN: String? = null
     var urlList: List<String>? = null
}
Copy the code

The new keyword is not used for initialization

Var tempEntity = Entity() var tempEntiy=new Entity();

Tempentity. name=” This is my name “// use the inside property to also click out

In Java, you can have multiple constructors, all of which are class names. There are some differences in Kotlin

  1. There is a distinction between primary and secondary constructors
  2. The primary and secondary constructors are recommended to exist separately because they require special handling to exist simultaneously
  3. There’s an initialization function, and as soon as you create this class, everything in that initialization function is executed

How to modify the access to the constructor?

The secondary constructor is written directly in front, and the primary constructor is written before the function and after the class name

class Entity {
    init {
        Log.e("TAG,"."Class initialization function, keyword is init- system built-in")}var name: String? = null
    var urlJUEJIIN: String? = null
    var urlCSDN: String? = null
    var urlList: List<String>? = null

    constructor(a) {}private constructor(name: String) {}// Secondary constructors for different arguments
    constructor(name: String, age: Int) {
        Log.e("TAG,".Constructor $name===$age)}}// These are two classes in two files

// This is the primary constructor, using the keyword constructor directly after the class name
class Person private constructor(name: String.age: String) {
    init {

    }

    // Special handling: if the primary construct and the secondary construct exist at the same time -- the parameter content of the primary construct must be included
    constructor(name: String, age: String, height: Int) : this(name, age) {

    }
}
Copy the code

/ / 14:52:59 2021-10-14. 468. 10639-10639 / cn appstudy E/TAG: Class init- system built-in // 21-10-14 14:52:59.468 10639-10639/ cn.appStudy E/TAG,: constructor test ===20

What about class inheritance?

If the parent class has a constructor, the subclass must also have at least one, and a warning will be given if there is not enough editor

Methods that use the superclass also use the super keyword, and the superclass requires the open keyword

open class Entity {
    init {
        Log.e("TAG,"."Class initialization function, keyword is init- system built-in")
    }

    constructor() {

    }

    constructor(name: String) {

    }

    // Secondary constructors for different arguments
    constructor(name: String, age: Int) {
        Log.e("TAG,".$name===$age)}}class EntityTwo : Entity {
    constructor() {

    }

    constructor(name: String) : this(name, 0) {}// Secondary constructors for different arguments
    constructor(name: String, age: Int) : super(name, age) {
        Log.e("TAG,".Subclass constructor $name===$age)}}Copy the code

👉 other

📢 author: Kom and Kom in Kom

📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

📢 welcome to like 👍 collect 🌟 message 📝