公开标签 #Kotlin
枚举类成员 import kotlin.reflect.full.memberFunctions import kotlin.reflect.full.memberProperties fun main(arg: Array<String>) { val p = Person::class //类引用 println("成员列表(属性和函数)" + p.members.size) for (member in p.members) { println(member.name + " " + member
结合函数声明: String getString(@IntRange(from = 0) int columnIndex); 可以知道getString方法要求一个int类型的参数,且要求该最小为0 getColumnIndex的声明: @IntRange(from = -1) int getColumnIndex(String columnName); 很明显它的返回值是从-1开始的 编译器检查到你传入getSring的参数存在不符合要求的可能性,就报错提醒你咯。 解决方法 在实际开发过程中,为了防止
什么是协程 先来看官方对 Kotlin 协程的介绍:Coroutines basics A coroutine is an instance of suspendable computation. It is conceptually similar to a thread, in the sense that it takes a block of code to run that works concurrently with the rest of the code. However, a cor
coroutines如其名,是kotlin提供一种轻量级的协程API,也可以理解为kotlin提供的异步操作API。 学习主要是阅读官方提供文档,圈下自己感觉的重点。 The main difference between [runBlocking]and [coroutineScope]is that the latter does not block the current thread while waiting for all children to complete. runblocking和
Kotlin可见修饰符使用 kotlin可见修饰符和java中的类似,同样使用的是public 、protected、private修饰符,不过在kotlin中默认的是public,在java中默认的修饰符是包私有,在kotlin中只把包当作在命名空间中组织代码的一种方式使用,并没有对其做可见性控制,而是提供了一个新的修饰符,internal来替代。 修饰符 类成员 顶层声明 public 所有地方可见 所有地方可见 internal 模块内可见 模块内可见 protected 子类中可见 不可用 pr
1.概述 在本教程中,我们将深入研究Kotlin枚举。 随着编程语言的发展,枚举的使用和应用也得到了发展。 如今,Enum常量不仅是常量的集合,它们还可以具有属性,实现接口等等。 对于Kotlin初学者,请查看有关Kotlin基础知识-Kotlin语言简介的文章。 2.基本的Kotlin枚举 让我们看一下Kotlin中枚举的基础。 2.1。定义枚举 让我们将枚举定义为具有三个描述信用卡类型的常量: enum class CardType { SILVER, GOLD, PLATINUM } 2.2。初始
有一天看到项目中的Kotlin类,有的有.kt后缀,有的没有,针对这个情况我就简单看了下,然后记录一波 创建 Kotlin Class 或 Kotlin File 创建Kotlin class 创建Kotlin File 俩者区别 展现形式 外部展现 内部展现 延伸扩展、对向转换 Class无后缀 → File有后缀 File有后缀 → Class无后缀 结论: 来这里看看 .kt后缀 的出现场景、区别 创建 Kotlin Class 或 Kotlin File 新建操作:包名 - 右键 - New -
/** * RecyclerView滚动到指定位置 */ var mToPosition:Int = 0 // 记录目标项位置 var mShouldScroll:Boolean = false // 目标项是否在最后一个可见项之后 fun smoothMoveToPosition(mRecyclerView: RecyclerView,position:Int){ // 第一个可见位置 var firstItem = mRecyclerView.getChildLayoutPosition(mRecy
示例1:使用格式对数字取整 示例 fun main(args: Array<String>) { val num = 1.34567 println("%.4f".format(num)) } 运行该程序时,输出为: 1.3457 在上面的程序中,我们使用format()方法将给定的浮点数打印num到4个小数位。.4f 表示小数点后4位格式。 这意味着,最多只打印点后的 4个位置(小数位),f表示打印浮点数。 示例2:使用DecimalFormat舍入数字 示例 import java.math.Rou
一、排序的简单用法 在Java中,对List进行排序可以使用Collections.sort()方法,在Kotlin中有非常简单的语法糖,所以这里记录一下,开发中尽量用简单的方式,方便维护。 一段未排序的示例代码如下: data class User(val name: String, val isOnline: Boolean) fun main() { val user1 = User("1", false) val user2 = User("2", false) val user3 = User