当前位置:网站首页>kotlin 初始化块
kotlin 初始化块
2022-06-24 12:50:00 【华为云】
@[TOC](kotlin 初始化块)
前言
使用纯代码 加 注释的方式,可以更快的理解源码
如果你喜欢,请点个赞,后期会不断的深入讲解
1、什么是初始化块 init 和主、 次构造函数
User(name = "张三", age = 21, sex = '女') // 这里会调用主构造函授 User(name = "张三", age = 21, sex = '女', height = 180) // 这里会调用次构造函授 // user 属性是主构造函数class User(name: String, age: Int, sex: Char) { // 这个不是Java 中的 static {}// 相当于是Java的 {} 构造代码块// 在kotlin 中, 是初始化块, init 构造代码块 init { println("主构造函数被调用了 $name, $age, $sex") } // constructor 是主构造函数 user 的 次构造函数 constructor(name: String, age: Int, sex: Char, height: Int) : this("tiger", 18, '男'){ println("次构造函数被调用了") }}2、构造函授初始化顺序
User(name = "张三", age = 21, sex = '女', height = 180)// 第一步:调用主构造函数class User(_name: String, age: Int, sex: Char) {// 第二步: 生成val nName (其实这个成员变量和 init 是同时生成的,只是 val nName = _name 写在了init的前面,所以才先执行) val nName = _name; init { val nameValue = nName; // 第三步: 生成 nameValue 的细节 println("init 代码块打印: $nameValue") } constructor(name: String, age: Int, sex: Char, height: Int) : this("tiger", 18, '男'){// 第四步: 生成次构造的细节 println("次构造函数被调用了") }}3、延迟初始化 lateinit
lateinit 在使用的时候,需要手动加载的懒加载
val lateManager = LateManager() lateManager.loadRequest() lateManager.showResponseResult()class LateManager{// lateinit val AAA; val 是不可修改函数,在后面都无法修改了,肯定不能用val lateinit var responseResultInfo: String // lateinit 懒加载模式,在使用的时候,才会被加载// 模拟服务器加载 fun loadRequest(){ responseResultInfo = "服务器加载成功!" } fun showResponseResult(){// 由于responseResultInfo 没有初始化,一调用就会直接导致崩溃,这里我们使用一个判断,判断 responseResultInfo 是否初始化 if (::responseResultInfo.isInitialized){ println("responseResultInfo: $responseResultInfo") }else{ println("你都还么有初始化,是不是忘了!") } }}4、惰性初始化 by lazy
惰性初始化 by lazy 是在使用的时候,自动加载的 懒加载方式
val serverData = ServerData()// 模拟数据库的加载,写一个定时器 Thread.sleep(5000) println("开始加载数据") println(serverData.dataBase)class ServerData{ val dataBase by lazy { readSqlServerDatabaseAction() } private fun readSqlServerDatabaseAction(): String { println("数据加载中,请等待。。。。。。") println("数据加载中,请等待。。。。。。") println("数据加载中,请等待。。。。。。") println("数据加载中,请等待。。。。。。") println("数据加载中,请等待。。。。。。") println("数据加载中,请等待。。。。。。") println("数据加载中,请等待。。。。。。") println("数据加载中,请等待。。。。。。") return "database data load success ok"; }}5、初始化陷阱 01
class ServerData{ // 在这里,看上去,代码没有任何问题,但是我们前面讲了,成员变量 number 和 init 是同级的。// 那么就意味着 init 执行的时候,成员变量 number 还没有初始化// 这里不要套入Java的思想,而是以顺序的思想去考虑 init { number = number.times(9) // times(9) 指 number * 9 } var number = 9}6、初始化陷阱 02
class ServerData{// 初始化的顺序 ServerData -> var info: String -> init -> getInfoMethod() -> info = "tiger"// 这样会直接造成info 返回的是一个null ,因为在进入 init 之后,直接调用的就是getInfoMethod()方法,这时候 成员变量 info 还没有进行初始化赋值 var info: String init { getInfoMethod() info = "tiger" } private fun getInfoMethod() { println("info: $info") }}7、初始化陷阱 03
// 这样写,看着没有什么问题,但是结合函数里面的代码,会直接导致崩溃// 当你调用 主构造函数,传入info, 再执行成员变量,调用getInfoMethod() 函数,获取字符长度的时候,其实你获取的是null println("内容的字符长度是:${User("tiger").content.length}")class User(_info: String) { val content = getInfoMethod() private val info = _info private fun getInfoMethod() = info}总结
🤩
️
边栏推荐
- Memory introduction
- TCP triple handshake
- Gateway processing flow of zuul source code analysis
- Kubernetes cluster deployment
- Evolution of the message module of the play live series (3)
- go Cobra命令行工具入门
- Getting started with the lvgl Library - colors and images
- Getting started with the go Cobra command line tool
- Why did the audio and video based cloud conference usher in a big explosion of development?
- 强化学习之父Richard Sutton论文:追寻智能决策者的通用模型
猜你喜欢

LVGL库入门教程 - 颜色和图像

About the hacked database

Parti,谷歌的自回归文生图模型

爱可可AI前沿推介(6.24)

工业物联网(IIoT)的八个主要趋势

我从根上解决了微信占用手机内存问题

Use abp Zero builds a third-party login module (I): Principles

CVPR 2022 | 美團技術團隊精選論文解讀

使用 Abp.Zero 搭建第三方登录模块(一):原理篇

Preparation and operation & Maintenance Guide for 'high concurrency & high performance & high availability service program'
随机推荐
openGauss内核:简单查询的执行
"I, an idiot, have recruited a bunch of programmers who can only" Google "
Parti,谷歌的自回归文生图模型
Brief introduction to cluster analysis
Getting started with the go Cobra command line tool
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
Why does the kubernetes environment require that bridge NF call iptables be enabled?
Vipshop's "special sale" business is no longer easy to do?
Prometheus PushGateway 碎碎念
How to efficiently analyze online log
The agile way? Is agile development really out of date?
[data mining] final review (sample questions + a few knowledge points)
工业物联网(IIoT)的八个主要趋势
CVPR 2022 | 美团技术团队精选论文解读
[one picture series] one picture to understand Tencent Qianfan ipaas
CVPR 2022 | interprétation de certains documents de l'équipe technique de meituan
8 - Format integers and floating point numbers
我从根上解决了微信占用手机内存问题
MySQL interview questions
How can junior middle school developers effectively reduce their own workload?