- Unit 类型
- Nothing 和 Nothing? 类型
- Any 和 Any? 类型
Kotlin中的 Unit类型 实现了Java中的void同样的功能。
当一个函数没有返回值的时候,使用 Unit 来表示这个特质;
Unit 父类型是 Any, Unit? 父类型是 Any?.
Nothing 和 Nothing? 类型
如果一个函数的返回值是 Nothing ,这意味着这个函数永远不会有返回值。
Nothing 可以用来表示一个不存在的返回值,如抛出异常。
- Unit 表示返回的数据类型是Unit类型;
- Nothing 表示永远不会有返回。
Nothing? 类型数据的值只能是 null.
Any 和 Any? 类型Any 是非空类型层次的根;
Any? 是可空类型层次的根;
Any? 是Kotlin类型层次结构的最顶端;
Any? 是 Any 的父类型。
fun testAnyF(): Unit {
val a = 1 is Any // Int类型的 1 是 Any类型
println(a) // true
val b = 1 is Any? // Int类型的 1 是 Any?类型
println(b) // true
val c = null is Any // null 不是 Any类型
println(c) // false
val d = null is Any? // null 是 Any? 类型
println(d) // true
val e = Any() is Any? // Any() 是 Any? 类型
println(e) // true
}



