kotlin
文章平均质量分 55
LJLThomson
专注移动端开发,拓展新知识,更好的提升自己
展开
-
kotlin遗留问题
kotlin遗留问题标准数据类标准库提供了 Pair 与 Triple。尽管在很多情况下具名数据类是更好的设计选择, 因为它们通过为属性提供有意义的名称使代码更具可读性。协变和逆变属性委托序列set集合: 可以去重,1,LinkedHashset : 保证元素添加的自然顺序2,TreeSet : 保证元素的自然顺序3.HashSet 是按照hash算法排序的,...原创 2021-07-19 13:21:11 · 94 阅读 · 0 评论 -
11kotin之协程(三)
kotlin之协程(三)1.CoroutineStart几种状态模式默认是CoroutineStart.DEFAULT 立即执行LAZY:稍后执行,只有执行job.start/join或者await才会开始执行package day8Coroutinesimport kotlinx.coroutines.CoroutineStartimport kotlinx.coroutines.asyncimport kotlinx.coroutines.delayimport kotlinx.cor原创 2021-07-16 09:52:58 · 158 阅读 · 0 评论 -
10kotlin之协程(二)
kotlin之协程(二)1.协程地取消与超时isActive,隐含了CoroutineScope,本身是挂起函数。 当取消时,使用它的位置,遇到挂起函数,自动计算协程状态package day8Coroutinesimport kotlinx.coroutines.*/** * 协程取消时,有一种情况,是无法取消的, 该函数正在计算中 * 取消的原理,只有当遇到挂起函数时,会自动计算当前协程状态 * */fun main(args: Array<String>) {原创 2021-07-16 09:52:42 · 157 阅读 · 0 评论 -
09kotlin之协程(一)
协程1.什么是协程协程是一种微线程,运行在线程中,但是协程与协程之间是不能直接通信的。2.runBlocking实际开发中基本不会用,阻塞线程,直到runBlocking内协程执行完毕后线程才能调用。fun main(args: Array<String>) { runBlocking { // this: CoroutineScope launch { // launch new coroutine in the scope of runBlocking原创 2021-07-15 14:32:48 · 138 阅读 · 0 评论 -
08kotlin之反射
kotlin之反射1.函数方法引用package day7refelect/** * 在包中引用 ::multiplyBy表示函数类型(int)->int * 支持重载 */fun multiplyBy(x: Int): Int { return x}fun multiplyBy(s: String): Int { return 2}fun main(args: Array<String>) { val values = listOf(1原创 2021-07-15 14:32:31 · 341 阅读 · 0 评论 -
07kotlin之java换转
kotlin和java互转平台类型:指的是java中ArrayList,HashSet等等1.集合 var mlist = ArrayList<String>(); mlist.add("A") mlist.add("B") for (item in mlist) { println(item) }2.类对象package day6kotlinCallJava;public class Person { private原创 2021-07-15 14:32:14 · 631 阅读 · 2 评论 -
06kotlin集合
kotlin集合1.简介kotlin中,集合分为可变和不可变但可读的集合2.集合1.mutableListOf可读可边集合,等价于java中的ArrayList。2.listOf可读不可变集合,不可添加元素进去。3.HashSetOfHashSet集合,元素不可重复,按照hashcode排序。val setList: HashSet<String> = hashSetOf("B", "A")4.mapof val numbers2 = mutableListOf("o原创 2021-07-13 09:30:39 · 440 阅读 · 0 评论 -
05kotlin之lamada
kotlin之lamada以及接收者类型1.函数l默认参数fun read(b: String, off: Int = 0, len: Int = b.length) { println("$b$off$len")}fun main(args: Array<String>) { read("ab") foo2(b = "nihao") // 具名参数}2.vararg函数表达式fun double(x: Int): Int = x * 2fun dou原创 2021-07-13 09:30:17 · 123 阅读 · 0 评论 -
04kotlin数据类与对象声明委托
数据类于对象声明1.数据类/** * 编译器自动从主构造函数中声明的所有属性导出以下成员: * equals()/hashCode() 对; * toString() 格式是 "User(name=John, age=42)"; * componentN() 函数 按声明顺序对应于所有属性; * copy() 函数(见下文)。 */data class User(val name: String, val age: Int)//如果在数据类体中有显式实现 equals()、原创 2021-07-13 09:29:53 · 145 阅读 · 0 评论 -
03kotlin扩展函数
扩展函数和属性1.介绍扩展:扩展一个类的新功能而无需继承该类或者使用像装饰者这样的设计模式,更多时候就是等价于java中静态函数。fun String.changeChar() = this[0] //this对应这个字符串,并推导最后的类型,并返回2.声明和this声明一个扩展函数,我们需要用一个 接收者类型 也就是被扩展的类型来作为他的前缀。隐含持有this,this表示接收者对象。/** * 这个 this 关键字在扩展函数内部对应到接收者对象(传过来的在点符号前的对象) * 很原创 2021-07-12 19:03:42 · 136 阅读 · 0 评论 -
02kotlin构造函数与接口
kotlin构造函数与接口1.构造函数1.constructorconstructor,有private等修饰时,需要/** * 主构造函数 * 一个类对象中,属性如果时val修饰,只生成 */class Person constructor(firstName: String) { /*……*/ }/** * 简化写法,如果没有private或者注解修饰则可以省略constructor关键字 */class Person2(firstName: String) {}class原创 2021-07-12 19:03:21 · 374 阅读 · 2 评论 -
01kotlin基础语法
koltin基础语法1.函数基础知识fun main() { println("Hello world") var value = sum1(3, 5) print(value)}//函数fun sum(a: Int, b: Int): Int { return a + b}//函数lamada表达式fun sum1(a: Int, b: Int) = a + b //lamada=会自动识别类型,如果块状//unit返回无类型,与java中void等原创 2021-07-12 19:01:22 · 93 阅读 · 0 评论