当前位置:网站首页>延迟初始化和密封类
延迟初始化和密封类
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 来添加对所有子类对应条件的处理。

边栏推荐
- IOT -- interpreting the four tier architecture of the Internet of things
- Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
- Understanding of law of large numbers and central limit theorem
- Circular reference of ES6 module
- All the ArrayList knowledge you want to know is here
- 你想知道的ArrayList知识都在这
- C语言自定义类型:结构体
- leetcode刷题 (5.31) 字符串
- Yyds dry goods inventory three JS source code interpretation eventdispatcher
- [research materials] 2022 China yuancosmos white paper - Download attached
猜你喜欢

让学指针变得更简单(三)

Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
![[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 ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
![[research materials] 2021 Research Report on China's smart medical industry - Download attached](/img/c8/a205ddc2835c87efa38808cf31f59e.jpg)
[research materials] 2021 Research Report on China's smart medical industry - Download attached

ESP series pin description diagram summary

Beijing invitation media

Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center

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

Pointer advanced --- pointer array, array pointer
随机推荐
让学指针变得更简单(三)
使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
Use dumping to back up tidb cluster data to S3 compatible storage
Migrate data from a tidb cluster to another tidb cluster
Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund
Let the bullets fly for a while
Make learning pointer easier (3)
logback1.3. X configuration details and Practice
"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
远程存储访问授权
On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
Circular reference of ES6 module
matplotlib. Widgets are easy to use
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
PLT in Matplotlib tight_ layout()
从 CSV 文件迁移数据到 TiDB
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
将 NFT 设置为 ENS 个人资料头像的分步指南
LDAP application (4) Jenkins access
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship