当前位置:网站首页>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())
}
边栏推荐
- Zen project management software is an indispensable tool for agile development teams
- Leetcode binary tree class
- UE5 GAS 学习笔记0.2配置插件
- How does Xiaobai learn software testing with zero foundation?
- leetcode 二叉树类
- Zero knowledge proof: zkp with DDH assumption
- MYSQL入门与进阶(十)
- Ue5 gas learning notes 0.1 case Preview
- AI 改变千行万业,开发者如何投身 AI 语音新“声”态
- 冒泡排序和相关视频
猜你喜欢

Three minutes to understand, come to new media

Zero knowledge proof: zkp with DDH assumption

Gateway入门

MYSQL入门与进阶(八)

The switching language of unity causes an error: system FormatException:String was not recognized as a valid DateTime.

SQL Server stuff and for XML path

@The difference between Autowired and @resource

什么样的知识付费系统功能,更有利于平台与讲师发展?

JVM four reference types

微信安装包11年膨胀575倍,UP主:“98%的文件是垃圾”;苹果应用商店被曝大量色情App;四大科技巨头呼吁废除闰秒|极客头条...
随机推荐
Zen project management software is an indispensable tool for agile development teams
LeetCode_96_不同的二叉搜索树
Is it really realistic that people who have not been exposed to software testing can take up their posts after two months of training?
MYSQL入门与进阶(二)
1.3 linked list
MySQL日期函数
注意力机制及代码实现
1.2 queue
UE5 GAS 学习笔记 1.1能力系统组件Ability System Component
十进制转二进制进阶版(可转化负数以及边界值)
视频融合云服务EasyCVR平台白名单功能如何使用?
Introduction and advanced level of MySQL (10)
JVM four reference types
@Autowired与@Resource区别
Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
微信安装包11年膨胀575倍,UP主:“98%的文件是垃圾”;苹果应用商店被曝大量色情App;四大科技巨头呼吁废除闰秒|极客头条...
What is one hot code? Why use it and when?
How does Xiaobai learn software testing with zero foundation?
Pyqt5 rapid development and practice 5.3 multithreading
What is the employment prospect of software testing?