Sealed Kotlin Kotlin] [sealed class (class declaration sealing | sealed class subclass definition | sealed class characteristic | Code Example)



I. Concept sealed class



1 sealed class action: define a sealed class that can have only a limited number of specified subclasses, subclasses of other types can not be defined in other documents;


2 sealed class and enumeration class:


① the same point (type restrictions): comparative From the perspective of type class, and class enumeration class Similarly, a set of values enumeration class is limited and can not expand;

② different from (limiting number of objects): Compare the number of each type of object, each type of enumeration class exists only an example, and each type of seal can create an unlimited number of class instances;



II. Seals class declaration



1 sealed class declaration: Add the sealed modifier before the class, the class can be declared to be sealed class;

sealed class Student{}

. Sub-classes: a subclass of class seal, the seal must be defined within the class; (later version 1.1 can be defined in the same file)

sealed class Student {

    class GoodStudent : Student()
}

For compatibility version or class is defined inside the sealing bar;



III. Note sealed class



1 essentially abstract class: sealed classes is essentially an abstract class that can not be instantiated type, only examples of its subclasses;


2. Private constructors: constructor is private sealed class default private, it must be private constructor does not allow the presence of non-private constructor;


3. seal Subclass:


① defined inside sealed class (recommended): subclass sealed class defined inside the sealing recommended class to be compatible with all versions;

② defined in the seal where the class file: same file Kotlin 1.1 later version may be sealed in a sealed class the subclass definition in the class declaration;


4 grandchildren sealed class categories: seal Subclass may be inherited, inherited class does not limit its grandchildren, can inherit the definition of other classes;


When statement. 5: a when statement is determined, there must be other cases reveal all else, it is determined that the sealed class subclasses, if all of the enumerated When Subclass sealing, it is then possible to write else branch, see details below sample code;



IV. Sealed class Sample Code



package sealed

sealed class Student {

    /**
     * 注意 : 只有被 open 修饰的函数才能被 override 重写
     */
    open fun study(){
        println("学习")
    }

    /**
     * 子类 1
     */
    class GoodStudent : Student(){
        override fun study() {
            println("学习很好")
        }

        fun read(){
            println("读书")
        }
    }

    /**
     * 子类 2
     */
    class NormalStudent : Student(){
        override fun study() {
            println("学习一般")
        }

        fun seat(){
            println("静坐")
        }
    }

    /**
     * 子类 3
     */
    class BadStudent : Student(){
        override fun study() {
            println("学的很渣")
        }

        fun play(){
            println("打游戏")
        }
    }

}

fun main() {

    // 1 . 测试密封类子类 1


    var goodStudent : Student.GoodStudent = Student.GoodStudent()

    //学习很好
    goodStudent.study()
    //读书
    studentAction(goodStudent)


    //2 . 测试密封类子类 2


    var normalStudent : Student.NormalStudent = Student.NormalStudent()

    //学习一般
    normalStudent.study()
    //静坐
    studentAction(normalStudent)


    //3 . 测试密封类子类 3
    

    var badStudent : Student.BadStudent = Student.BadStudent()

    //学的很渣
    badStudent.study()
    //打游戏
    studentAction(badStudent)


}

/**
 * 根据不同的类型执行不同的方法
 */
fun studentAction(student : Student) = when(student){

    //如果已经覆盖了 3 个子类 , 即所有的情况 , 此时可以不需要定义 else 语句
    is Student.GoodStudent -> student.read()
    is Student.BadStudent -> student.play()
    is Student.NormalStudent -> student.seat()

    //else -> println("其它情况")
}

Results of the :

学习很好
读书
学习一般
静坐
学的很渣
打游戏
Published 307 original articles · won praise 1043 · Views 1.7 million +

Guess you like

Origin blog.csdn.net/han1202012/article/details/105034490