Kotlin & Java 语法对比
Kotlin & Java 语法对比
null safe
1 | val userName = a?.b?.c?.d ?: "" |
对象实例化
1 | class User(val name: String, val age: Int) |
1 | public class User { |
属性访问
1 | class User { |
1 | public class User { |
默认的构造函数
1 | class User(var name: String) |
1 | public class User { |
快输创建 List/Map 集合类型
1 | // list |
1 | // list |
对象属性调用(with\apply\let)
1 | val user = User() |
1 | User user = new User(); |
Android 自动绑定 XML 文件控件定义
1 | import kotlinx.android.synthetic.main.activity_main.* |
简化 Parcelable 实现
1 | import android.os.parcelable |
协程 coroutines
1 | GlobalScope.launch { |
简化单例(类似与 Swift 的 struct)
1 | object User { |
1 | public final class User { |
使用字符串模板简化字符串操作
1 | fun getInfo():String = ">> $name; and age is ${age}" |
1 | public String getInfo() { |
使用 when 替代 switch & case
1 | var id = 1 |
1 | int id = 1; |
解构对象(Destructing declarations)类似 JS 中的 rest
1 | data class User(val userName: String, val age: Int) |