Kotlin learning: 3.6 Classification of classes

Posted by Mew151 on Wed, 09 Feb 2022 14:18:33 +0100

Class modifier

Class modifiereffect
finalCannot be inherited from final class
openopen class that can be inherited
abstractabstract class
enumEnumeration class
dataData class
sealedSealing class
annotationAnnotation

abstract class

Use the abstract keyword to mark a class as abstract

abstract class Abstract class name[(primary constructor )][:Inherit parent classes and implement interfaces]{
	abstract fun Method name
	abstract var/val Attribute name: attribute type
  	...
}

Chestnut

father

abstract class AbBaseFruit {

    constructor(name: String) {
        this.name = name;
    }

    var name: String = ""

    abstract val TIME: Int

    abstract var size: Int

    abstract fun eat()

//    abstract fun wash(){
//    //The wrong method cannot be decorated
//    }

    fun wash() {

    }

    fun print(){
        println("name = $name")
        println("TIME = $TIME")
        println("size = $size")
    }

}

son

class AbApple(name: String) : AbBaseFruit(name) {

    override val TIME: Int
        get() = 123

    override var size: Int
        get() = 10
        set(value) {
            size = value
        }

    override fun eat() {
    }

}

call

        val apple2 = AbApple("Test abstract class")
        apple2.print()

output

2022-02-09 11:06:05.591 24363-24363/com.example.kotlintestdemo I/System.out: name = Test abstract class
2022-02-09 11:06:05.592 24363-24363/com.example.kotlintestdemo I/System.out: TIME = 123
2022-02-09 11:06:05.592 24363-24363/com.example.kotlintestdemo I/System.out: size = 10

Enumeration class

enum class Class name{
    Enumeration constant list
}

Chestnut

enum class ColorS {
    RED, BLACK, BLUE, GREEN, WHITE
}

Data class

1. Automatic implementation

As long as it is a data class, the following methods will be automatically implemented:

equals() / hashCode()
toString() Such as format "User(name=John, age=42)"
componentN() functions Corresponding to attributes, in the order of declaration
copy() function

2. Constraints

Data class requirements (constraints)

The main constructor contains at least one parameter
The parameters of all primary constructors must be identified as val or var
Data classes cannot be declared as abstract, open, sealed, or internal
Data classes cannot inherit other classes, but can implement interfaces.

3. Chestnut

data class DataExample(var name: String ,var age :Int) {

}

call

        val dataExample = DataExample("Xiao Ming",12)
        println("dataExample === $dataExample")
        dataExample.name = "Oguri "
        println("dataExample === $dataExample")
        val dataExample2 = dataExample.copy(name = "Xiao Wang")
        println("dataExample2 === $dataExample2")

Output

2022-02-09 14:28:58.776 29288-29288/com.example.kotlintestdemo I/System.out: dataExample === DataExample(name=Xiao Ming, age=12)
2022-02-09 14:28:58.776 29288-29288/com.example.kotlintestdemo I/System.out: dataExample === DataExample(name=Oguri , age=12)
2022-02-09 14:28:58.776 29288-29288/com.example.kotlintestdemo I/System.out: dataExample2 === DataExample(name=Xiao Wang, age=12)

4. Deconstruction statement

In Koltin, you can assign an object to multiple variables. This operation is called deconstructing declaration

        val (name,age)=dataExample  //Deconstruction statement
        println("Deconstruction statement == $name,$age")

output

2022-02-09 14:35:58.331 30036-30036/com.example.kotlintestdemo I/System.out: Deconstruction statement == Oguri ,12

Sealing class

A sealed class is used to represent a restricted class inheritance structure: when a value has a limited number of types and cannot have any other types. In a sense, they are extensions of enumeration classes: the value collection of enumeration types is also limited, but there is only one instance of each enumeration constant, while a subclass of a sealed class can have multiple instances of states.

Declare a sealed class and use sealed to decorate the class. A sealed class can have subclasses, but all subclasses must be embedded in the sealed class.

sealed cannot modify interface and abstract class (warning will be reported, but no compilation error will occur)

The key advantage of using sealed classes is that when using the when expression, if you can verify that the statement covers all cases, you don't need to add an else clause to the statement.

Kotlin data class and seal class:
https://www.runoob.com/kotlin/kotlin-data-sealed-classes.html

Kotlin volatile | sealed class
https://zhuanlan.zhihu.com/p/122244787

Annotation

Nested class

As long as a class is defined in another class, this class becomes a nested class, which is equivalent to the static internal class modified by static in Java.

It has no external references

class KtExample(name: String) {
    class NestingClass {
        fun log(){
            
        }
    }
}

call

KtExample.NestingClass ().log()

Inner class

Nested classes decorated with inner are called inner classes, which is equivalent to non static inner classes without static decoration in Java.

The inner class is equivalent to the instance members of the outer class, so it can directly access all members of the outer class.

class KtExample(name: String) {
    inner class NestingClass {
        fun log(){

        }
    }
}

call

KtExample().NestingClass ().log()

Nested and inner classes of Kotlin
https://blog.csdn.net/weixin_40763897/article/details/107825116

Topics: Java kotlin