Kotlin type advanced

Time:2022-3-2

1、 Foreword

< font face = bold > inKotlin generics (Part 1)In, we have put kotlin’sGeneric basisAfter that, to learn about advanced generics, we need to understand several classes first.

2、 Data class

2.1 definition of data class

< font face = bold > just add a data in front of an ordinary class, and the ordinary class will become a data class. You can compare it with the JavaBean of Java, as follows:

data class Book(val id: Long, val name: String, val author: Person)

< font face = bold > the attribute defined in the column constructor in the above code is also called component. You can call book The component method gets the value of the attribute in the constructor.

val id = book.component1()
val name = book.component2()
val author = book.component3()

2.2、JavaBean vs data class

JavaBean data class
Construction method Default parameterless construction Property as a parameter
field Field private, getter / setter public attribute
Inheritance Can inherit or be inherited Not inheritable
component nothing Want to be equal, deconstruct, etc

2.3. How to use data class reasonably

< font face = bold > let’s take a look at the properties of data class, as follows:
Kotlin type advanced

< font face = bold > from the above figure, we know that we should use the data class as a data structure without additional implementation, and the attribute class should be basic and immutable to ensure that the data class does not generate logic.

3、 Enum class enum class

3.1 definition of enumeration

Java:

enum State {
    Idle, Busy
}

//Return the name idle through the name method
State.Idle.name()
//Return sequence number 0 through ordinal
State.Idle.ordinal()

Kotlin:

enum class State {
    Idle, Busy
}

//Return the name idle through the name method
State.Idle.name
//Return sequence number 0 through ordinal
State.Idle.ordinal

4、 Sealed class

4.1 concept of sealing

  • < font face = bold > sealed class is a special abstract class;
  • < font face = bold > the subclass of the sealed class is defined in the same file as itself;
  • < font face = bold > the number of subclasses of sealed class is limited.

4.2 definition of sealing class

sealed class PlayerState {
    //Constructor private
    constructor()
    constructor(int: Int)
}

5、 Inline class

  • Font < face > is a type of inline;
  • < font face = bold > inline class is a type similar to Java boxing type;
  • < font face = bold > the compiler will optimize using the type of wrapper as much as possible.

5.1 definition of inline class

//Packing int type, must be val
inline class BoxInt(val value: Int)

5.2. Methods of inline classes

inline class BoxInt(val value: Int) {
    operator fun inc(): BoxInt {
        return BoxInt(value + 1)
    }
}

5.3 inheritance structure of inline class

< font face = bold > inline classes can implement interfaces, but they cannot inherit the parent class or be inherited.

//It is possible to implement the interface
inline class BoxInt(val value: Int): Comparable<Int> {
    override fun compareTo(other: Int)
            = value.compareTo(other)
}

6、 Source code

Source codeIt has been uploaded to GitHub and can be downloaded directly if necessary.