官方版本
github
类型 | 大小(比特数) | 最小值 | 最大值 |
---|---|---|---|
Byte | 8 | -128 | 127 |
Short | 16 | -32768 | 32767 |
Int | 32 | -2,147,483,648 (-231) | 2,147,483,647 (231 - 1) |
Long | 64 | -9,223,372,036,854,775,808 (-263) | 9,223,372,036,854,775,807 (263 - 1) |
类型 | 大小(比特数) | 有效数字比特数 指数比特数 | 十进制位数 |
---|---|---|---|
Float | 32 | 24 | 8 |
Double | 64 | 53 | 11 |
类、对象、接口、构造函数、方法、属性和它们的 setter 都可以有 可见性修饰符
repeat(100) {
// todo
}
for (index in 1..100){
print(index)
}
for (index in 1 until 10){
println(index)//输出0..9
}
for (index in 1..100 step 2){
print(index)//会输出1..3..5......
}
for (index in 100 downTo 1){
print(index)
}
when (direction) {
0 -> setCompoundDrawables(drawable, null, null, null)
1 -> setCompoundDrawables(null, drawable, null, null)
2 -> setCompoundDrawables(null, null, drawable, null)
3 -> setCompoundDrawables(null, null, null, drawable)
else -> throw NoSuchMethodError()
}
val date = lesson.date?: "⽇期待定"
val name = "haha"
val text = "${name}"
val text = "$name"// 如果只是单⼀的变量,可以省略掉 {}
在类型后面加上!,为平台类型,java通过注解释来减少平台类型的产生
// 声明抽象类
abstract class
// 声明接⼝
interface
// 声明注解
annotation class
// 声明枚举
enmu class
val strs: List = listOf("a", "b", "c")
val anys: List = strs // success
val strSet = setOf("a", "b", "c")
val map = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 3)
val map = mutableMapOf("key1" to 1, "key2" to 2)
map.put("key1", 2)
map["key1"] = 2
4.listOf() 创建不可变的 List,mutableListOf() 创建可变的 List。
setOf() 创建不可变的 Set,mutableSetOf() 创建可变的 Set。
mapOf() 创建不可变的 Map,mutableMapOf() 创建可变的 Map。
val strList = listOf("a", "b", "c")
strList.toMutableList()
val strSet = setOf("a", "b", "c")
strSet.toMutableSet()
val map = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 3)
map.toMutableMap()
Kotlin 使用data修饰实体类,数据类会同时构造出以下方法
对基本数据类型进行copy,对数据类型只是引用,没有真实创建一个新的对象,叫浅copy
对引用数据类型时候,创建一个新的对象,并复制其内的成员变量,则为深copy
可以把一个对象解构多个属性
val (code, message, body) = response
val code = response.component1()
val message = response.component2()
val body = response.component3()
//class 替换成了 object
object A {
val number: Int = 1
fun method() {
println("A.method()")
}
}
//java调用匿名内部类
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
//kotlin使用object调用匿名内部类
userName.setOnClickListener(object :View.OnClickListener{
override fun onClick(p0: View?) {
TODO("Not yet implemented")
}
})
在类内部维护一个单例对象
class A {
companion object B {
var c: Int = 0
}
}
Kotlin构造函数,直接用constructor,取代java中和类同名
class CodeView constructor(context: Context) : TextView(context) {
init {
//setTextSize()
}
val paint = Paint() // 会在 init{} 之后运⾏
}
fun test()=println("test")
fun CharSequence.showToast(duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(EyepetizerApplication.context, this, duration).show()
}
函数放到类的外面,一般工具类的开发使用顶层函数,方便调用,开发重复;
使⽤ inline 关键字声明的函数是「内联函数」,在编译时会将「内联函数」中的函数体,直接插⼊到
调⽤出。内涵函数意义:
自定义中缀函数,用来做扩展函数
infix fun Int.add(x: Int): Int {
return this + x
}
println(100 add 200)
Kotlin高阶函数的使用
with函数接收两个对象, 第一个对象是任意类型的对象, 第二个参数是lambda表达式.
Lambda表达式中会提供第一个参数对象的上下文.
使用Lambda的最后一行代作为返回值返回