当前位置:网站首页>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}总结
🤩
️
边栏推荐
- 几种常见的DoS攻击
- Creation and use of unified links in Huawei applinking
- LVGL库入门教程 - 颜色和图像
- Several common DoS attacks
- Attack Science: ARP attack
- Preparation and operation & Maintenance Guide for 'high concurrency & high performance & high availability service program'
- On the difference between process and thread
- Quickly understand the commonly used message summarization algorithms, and no longer have to worry about the thorough inquiry of the interviewer
- "Interesting" is the competitiveness of the new era
- Opengauss kernel: simple query execution
猜你喜欢

首席信息安全官仍然会犯的漏洞管理错误

Kubernetes cluster deployment

3. caller service call - dapr

Getting started with the lvgl Library - colors and images

How stupid of me to hire a bunch of programmers who can only "Google"!

Quickly understand the commonly used message summarization algorithms, and no longer have to worry about the thorough inquiry of the interviewer

我真傻,招了一堆只会“谷歌”的程序员!

Yolov6: the fast and accurate target detection framework is open source

1、贪吃蛇游戏设计

#云原生征文#Ingress案例实战
随机推荐
Creation and use of unified links in Huawei applinking
What is the difference between sap QM and UD for inspection lots with hum?
LVGL库入门教程 - 颜色和图像
8 - Format integers and floating point numbers
The data value reported by DTU cannot be filled into Tencent cloud database through Tencent cloud rule engine
Teach you how to use airtestide to connect your mobile phone wirelessly!
谁是鱼谁是饵?红队视角下蜜罐识别方式汇总
‘高并发&高性能&高可用服务程序’编写及运维指南
What should I do if I fail to apply for the mime database? The experience from failure to success is shared with you ~
One hour is worth seven days! Ingenuity in the work of programmers
go Cobra命令行工具入门
LVGL库入门教程 - 颜色和图像
首席信息安全官仍然会犯的漏洞管理错误
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
Introduction to reptile to give up 01: Hello, reptile!
初中级开发如何有效减少自身的工作量?
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
The introduction of MySQL memory parameters is divided into two categories: thread exclusive and global sharing
Without home assistant, zhiting can also open source access homekit and green rice devices?
CVPR 2022 | 美团技术团队精选论文解读