当前位置:网站首页>延迟初始化和密封类
延迟初始化和密封类
2022-07-06 08:22:00 【故、梦】
延迟初始化
背景
Kotlin 提供了变量不可空特性来抑制空指针问题。
这个特性在保证程序安全的同时,也给我们带来了编码上的一些问题。
案例:试想一下,你的程序中有一个全局变量 a ,变量 a 创建之初并没有初始化,所以你令它等于 null。
假设 a 的类型为 String,因为要保证 a 非空,所以它的类型被声明为 String?
private var a: String? = null
接下来,每次需要调用 a 中的方法,都要进行判空处理(即使它早已被初始化),否则无法进行编译!
当代码中有越来越多的全局变量实例时,这个问题会愈发明显。
延迟初始化
为了解决这个问题,Kotlin 使用 lateinit 关键字对变量进行初始化。
private lateinit var a: String
lateinit 关键字告诉 Kotlin 编译器,我会在后续对该变量进行初始化,这样就不用将变量赋值为 null。
这样,在调用全局变量 a 中的方法时,就不用再进行判空处理。
在使用该对象时,需要保证其已被初始化,否则会出现运行时错误!
避免重复初始化
为了避免变量的重复初始化,可以使用以下代码
if (!::a.isInitialized) {
// 判断 a 是否初始化,如果未初始化执行代码块
a = "ming ming" // 对 a 进行初始化操作
}
::object.isInitialized 是判断初始化的固定写法。
密封类
背景
新建一个 Result.kt 文件,在文件中编写以下代码:
interface Result
class Success(val message:String):Result
class Failure(val error: Exception):Result
代码中定义了一个 Result 接口,用于表示某个操作的执行结果。
定义两个类去实现 Result 接口,Success 类用于表示成功时的结果,Failture 类用于表示失败时的结果。
再定义一个 getResultMessage(),用于执行最终执行结果的信息,代码如下
fun getResultMessage(result: Result) = when(result){
is Success -> result.message
is Failure -> result.error.message
else -> throw IllegalArgumentException()
}
getResultMessage() 方法接收一个 Result 参数。
我们用 when 语句来判断 Result 的类型,如果为 Success,打印成功消息,如果为 Failture 返回错误信息。
讲道理,此时应该结束代码。因为代码的执行结果要么成功,要么失败,并不存在其他情况。
但是为了满足 Kotlin 的语法检查,在最后写了一个 else 来抛出异常(实际上这个 else 是永远无法到达的)。
此外,如果我们新增了一个其他类继承 Result 接口,而忘了在 getResultMessage() 中添加对应的执行代码,
代码在执行时就会直接抛出 else 中的异常。
密封类
密封类可以很轻松地解决上述问题,声明一个密封类只需要在类前加上关键字 sealed。
将 Result 接口改造成密封类
sealed class Result
class Success(val message:String):Result()
class Failure(val error: Exception):Result()
接下来在 when 中传入密封类做为条件,Kotlin 会检查该密封类有哪些子类,并强制要求你去处理每个子类所对应的条件,这样就可以保证即使没有编写 else 条件,也不会出现漏写分支的情况。
fun getResultMessage(result: Result) = when(result){
is Success -> result.message
is Failure -> result.error.message
}
当我们新建一个 Others 类继承 Result 密封类,getResultMessage() 会报错。
我们可以通过 Add remaining branches 来添加对所有子类对应条件的处理。

边栏推荐
- Easy to use tcp-udp_ Debug tool download and use
- IOT -- interpreting the four tier architecture of the Internet of things
- 图像融合--挑战、机遇与对策
- Step by step guide to setting NFT as an ens profile Avatar
- Résumé des diagrammes de description des broches de la série ESP
- hcip--mpls
- Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation
- Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
- On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
- Analysis of Top1 accuracy and top5 accuracy examples
猜你喜欢

Configuring OSPF load sharing for Huawei devices

"Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco

Synchronized solves problems caused by sharing

"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa

2022.02.13 - NC003. Design LRU cache structure

指针和数组笔试题解析

Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform

在 uniapp 中使用阿里图标
![[research materials] 2021 live broadcast annual data report of e-commerce - Download attached](/img/a6/74da2f44c7b6b22fed2f8e41a55988.jpg)
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached

The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable
随机推荐
Leetcode skimming (5.29) hash table
【MySQL】数据库的存储过程与存储函数通关教程(完整版)
TiDB备份与恢复简介
升级 TiDB Operator
Hill sort c language
Leetcode question brushing record | 203_ Remove linked list elements
面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
在 uniapp 中使用阿里图标
[MySQL] log
指针进阶---指针数组,数组指针
Let the bullets fly for a while
Pyqt5 development tips - obtain Manhattan distance between coordinates
Pointer advanced --- pointer array, array pointer
[2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
NFT smart contract release, blind box, public offering technology practice -- contract
On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
ESP series pin description diagram summary
Summary of MySQL index failure scenarios
Colorlog结合logging打印有颜色的日志
Nacos Development Manual