简介
sealed 修饰的类称为密封类,用来表示受限的类层次结构。
案例
代码语言:txt复制// 外部无法实例化密封的类
// 外部只能实例化他的子类
sealed class Color {
// 只能在内部继承密封类
class Red(val value: Int) : Color()
class Green(val value: Int) : Color()
class Blue(val name: String) : Color()
}
fun isInstance(color: Color) {
when (color) {
// 必须写全所有条件,否则报错
is Color.Red -> TODO()
is Color.Green -> TODO()
is Color.Blue -> TODO()
}
}
案例
代码语言:txt复制// 默认情况下,密封类的构造函数是私有的。
// 密封类的所有子类必须在同一文件中声明。
sealed class A {
class B : A() {
// this works.
class E : A()
}
class C : A()
init {
println("sealed class A")
}
}
//this works
class D : A() {
// This won't work. Since sealed class is defined in another scope.
class F: A()
}
案例
代码语言:txt复制// 在密封的类中添加数据类和对象
sealed class A {
class B : A()
class C : A()
object D : A() {
fun name() {
println("Object D")
}
}
data class E(var name: String) : A()
}
演示
代码语言:txt复制sealed class NetworkResult<out T> {
data class Success<out T>(val value: T) : NetworkResult<T>()
data class Failure(val throwable: Throwable?) : NetworkResult<Nothing>()
}
when (result) {
is PokemonResult.Failure -> {
// 进行失败提示
}
is PokemonResult.Success -> {
// 进行成功处理
}
}
代码语言:txt复制sealed class PrchaseResult {
sealed class GoogplayBilling: PrchaseResult(){
data class Sucess(val purchseToken : String) : GoogplayBilling()
data class Fail(val error : String) : GoogplayBilling()
object Retry : GoogplayBilling()
sealed class PurchaseAcknowledgement : GoogplayBilling(){
object Sucess : PurchaseAcknowledgement()
data class Fail(val errorCode : Int) : PurchaseAcknowledgement()
}
}
sealed class Stripe: PrchaseResult(){
data class Sucess(val purchseToken : String) : Stripe()
data class Fail(val error : String) : Stripe()
object Retry : Stripe()
sealed class PurchaseAcknowledgement : Stripe(){
object Sucess : PurchaseAcknowledgement()
data class Fail(val errorCode : Int) : PurchaseAcknowledgement()
}
}
sealed class PayYouMoney: PrchaseResult(){
data class Sucess(val purchseToken : String) : PayYouMoney()
data class Fail(val error : String) : PayYouMoney()
object Retry : PayYouMoney()
sealed class PurchaseAcknowledgement : PayYouMoney(){
object Sucess : PurchaseAcknowledgement()
data class Fail(val errorCode : Int) : PurchaseAcknowledgement()
}
}
}
when(signal){
// GooglePlay Billing related
is PrchaseResult.GoogplayBilling.Sucess -> { }
is PrchaseResult.GoogplayBilling.Fail -> { }
is PrchaseResult.GoogplayBilling.Retry -> { }
is PrchaseResult.GoogplayBilling.PurchaseAcknowledgement.Sucess -
is PrchaseResult.GoogplayBilling.PurchaseAcknowledgement.Fail ->
// PayYouMoney Billing related
is PrchaseResult.PayYouMoney.Sucess -> { }
is PrchaseResult.PayYouMoney.Fail -> { }
is PrchaseResult.PayYouMoney.Retry -> { }
is PrchaseResult.PayYouMoney.PurchaseAcknowledgement.Sucess -> { }
is PrchaseResult.PayYouMoney.PurchaseAcknowledgement.Fail -> { }
// PayYouMoney Billing related
is PrchaseResult.Stripe.Sucess -> { }
is PrchaseResult.Stripe.Fail -> { }
is PrchaseResult.Stripe.Retry -> { }
is PrchaseResult.Stripe.PurchaseAcknowledgement.Sucess -> { }
is PrchaseResult.Stripe.PurchaseAcknowledgement.Fail -> { }
}