大家好,我是William李梓峰,歡迎加入我的Kotlin學(xué)習(xí)之旅。
今天是我學(xué)習(xí) Kotlin 的第十四天,內(nèi)容是 Sealed Classes - 密封類。
官方文檔:
Sealed Classes - 密封類
Sealed classes are used for representing restricted class hierarchies, when a value can have one of the types from a limited set, but cannot have any other type.
密封類是用于表示等級(jí)嚴(yán)謹(jǐn)?shù)念悾?dāng)一個(gè)值可以從一個(gè)有限集中擁有一個(gè)類型而不能有其他類型,密封類就能發(fā)揮其作用。(寫的有點(diǎn)飄,靠譜的理解在下面~)
They are, in a sense, an extension of enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances which can contain state.
密封類,實(shí)際上是一個(gè)枚舉類的擴(kuò)展形式:集合的枚舉類型是嚴(yán)格要求的,但是每個(gè)枚舉常量?jī)H存在一個(gè)單例,而密封類的子類可以包含狀態(tài)。
To declare a sealed class, you put the sealed
modifier before the name of the class. A sealed class can have subclasses, but all of them must be declared in the same file as the sealed class itself. (Before Kotlin 1.1, the rules were even more strict: classes had to be nested inside the declaration of the sealed class).
為了聲明一個(gè)密封類,你可以在類的前面寫 sealed 修飾符。一個(gè)密封父類可以擁有密封子類,但是所有密封子類都必須聲明在同一個(gè)文件里面,該文件就是密封父類聲明所在的地方。(在 Kotlin 1.1 之前,這個(gè)規(guī)則更加苛刻:子類必須在父類內(nèi)部聲明,多惡心)
sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
(The example above uses one additional new feature of Kotlin 1.1: the possibility for data classes to extend other classes, including sealed classes.)
(上面的例子使用了 Kotlin 1.1的一個(gè)新特性:數(shù)據(jù)類可以繼承其他類,包括密封類)
Note that classes which extend subclasses of a sealed class (indirect inheritors) can be placed anywhere, not necessarily in the same file.
要知道的是,那些繼承了密封類的子類(間接繼承)可以放在任何地方,不一定是在同一個(gè)文件內(nèi)部。(例如數(shù)據(jù)類繼承了某個(gè)密封類,而這個(gè)數(shù)據(jù)類可以放置任何一個(gè)地方,不一定要在密封類所在的文件下)
The key benefit of using sealed classes comes into play when you use them in a when
expression. If it's possible to verify that the statement covers all cases, you don't need to add an else
clause to the statement.
使用密封類最關(guān)鍵的好處就是在 when 表達(dá)式 那里的應(yīng)用。如果判斷語(yǔ)句覆蓋了所有的密封類實(shí)例,則你不再需要額外寫 else 語(yǔ)句。(好像方便了一點(diǎn),不過(guò)說(shuō)真的,密封類相當(dāng)于枚舉類,唯一不同就是普通類可以繼承密封類,而枚舉類則不行。)
fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
// the `else` clause is not required because we've covered all the cases
}
這里官方文檔沒有給出豐富的密封類的使用例子,可能好戲在后頭,例如 Kotlin Koan。