当前位置:网站首页>Kotlin:sealed Class detailed explanation of sealed class
Kotlin:sealed Class detailed explanation of sealed class
2022-07-28 18:51:00 【Ming Fei 1987】
One : Definition of sealed class
Use before the class name sealed Keyword declares a class as a sealed class . The importance of sealing classes to restrict the set of types only at compile time to ensure type safety . A sealed class is implicitly an abstract class that cannot be instantiated .
Two : The characteristics of sealed classes and their differences from other classes
①Sealed class( Sealing class ) Is a class with a specific number of subclasses , It looks a little similar to enumeration , The difference is , In enumeration , We have only one object per type ( example ); In the sealed class , The same class can have several objects .
②Sealed class( Sealing class ) All subclasses of must be in the same file as the sealed class
③Sealed class( Sealing class ) Subclasses of subclasses of can be defined anywhere , It does not need to be defined in the same file as the sealed class
④Sealed class( Sealing class ) There is no constructor , You can't instantiate it directly , Only inner subclasses can be instantiated
Two : How to get function methods in sealed classes
Only sealed subclass objects can be created Call the function method in the sealed class through the subclass object of the sealed class
sealed class SealedClass{
class SealedClass1():SealedClass()
class SealedClass2():SealedClass()
fun hello(){
println("Hello World ... ")
}
}
Objects can only be instantiated through subclasses inside sealed classes , Then you can execute the methods inside
fun main(args:Array<String>){
var sc:SealedClass = SealedClass.SealedClass1()
sc.hello()
}
3、 ... and :
Sealed classes are usually associated with expressions when When used together . Because subclasses of sealed classes regard their own types as a case . therefore , In the sealed class when The expression covers all cases , So as to avoid using else Clause .
sealed class MyColor {
class Yellow : MyColor()
class Red : MyColor()
class Black : MyColor()
}
fun evals(color: MyColor) =
when (color) {
is MyColor.Yellow -> println("yellow")
is MyColor.Red -> println("red")
is MyColor.Black -> println("black")
}//todo No need to use else 了
fun main(args: Array<String>) {
evals(MyColor.Black())
}
边栏推荐
- Software testing needs more and more talents, but fewer people are on the road of testing?
- 专题讲座6 树形dp 学习心得(长期更新)
- leetcode 二叉树类
- EasyCVR设备离线后无法再次上线该如何解决?
- Go exe generates icon version information
- UE5 GAS 学习笔记0.1 案例预览
- MYSQL入门与进阶(七)
- PyQt5快速开发与实战 5.3 多线程
- Log base zap of go language series
- What skills do you need to master when learning software testing zero foundation?
猜你喜欢

What is one hot code? Why use it and when?

One Hot编码是什么?为什么要用它,什么时候用它?

1.3 linked list

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

Introduction and advanced level of MySQL (5)

Ue5 gas learning notes 0.2 configuration plug-in

jvm四种引用类型

1.1. Sparse array

Go concurrency one

历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...
随机推荐
EasyNLP中文文图生成模型带你秒变艺术家
Is it really realistic that people who have not been exposed to software testing can take up their posts after two months of training?
redis优势以及数据结构相关知识
Differences between RDB and AOF for redis persistence
Getting started with gateway
LeetCode_ 63_ Different paths II
LeetCode_63_不同路径Ⅱ
注意力机制及代码实现
Why app uses JSON protocol to interact with server: serialization related knowledge
2022-07-27 study notes of group 4 self-cultivation class (every day)
Ue5 gas learning notes 1.1 capability system component
Meta Q2 earnings: revenue fell for the first time, and metaverse will compete with apple
UE5 GAS 学习笔记 1.5 Gameplay Effects游戏效果
What skills do you need to master when learning software testing zero foundation?
配置教程:新版本EasyCVR(v2.5.0)组织结构如何级联到上级平台?
真正的 HTAP 对用户和开发者意味着什么?
What is one hot code? Why use it and when?
全新升级!《云原生架构白皮书 2022 版》重磅发布
MYSQL入门与进阶(七)
Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.