
学习Google官方文档的同时记录一下自己的翻译过程,以便后续再学习。不当之处,请批评指正!附原文:https://developer.android.com/kotlin/index.html?hl=zh-cn。
一、Kotlin and Android
Kotlin is now an official language on Android. It's expressive, concise, and powerful. Best of all, it's interoperable with our existing Android languages and runtime.
Kotlin 现在是开发Android的官方语言,它表现力强、简洁、有力。最重要的是,它兼容现在的Android语言和运行时。
二、Modern. Expressive. Safe.
Kotlin is concise while being expressive. It contains safety features for nullability and immutability, to make your Android apps healthy and performant by default.
Kotlin简洁而富有表现力。它包含可空性和不变性这样的安全特性,以使您的Android应用程序在默认情况下保持健壮性和高性能。
1. 更安全的代码
- 写更安全的代码,避免App内空指针异常。
//Kotlin
var output: String
output = null // Compilation error
==================================
val name: String? = null // Nullable type
println(name.length()) // Compilation error
2. 可读性和简洁性
- 数据类
专注于表达你的想法,写更少的样板代码。
//Kotlin
// Create a POJO with getters, setters, equals(), hashCode(), toString(), and copy()
// with a single line:
data class User(val name: String, val email: String)
- Lambdas
使用Lambdas简化代码
//Java
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
doSomething();
}
});
//Kotlin
button.setOnClickListener { doSomething() }
- 默认的和命名的参数
通过使用默认的参数减少重载方法的数量。调用方法时使用命名的参数提高代码可读性。
//Kotlin
fun format(str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' ') {
…
}
==================================
// Call function with named arguments.
format(str, normalizeCase = true, upperCaseFirstLetter = true)
3. 不再需要findViewById
代码中可以不再使用findViewById(), 代码更简洁。
//Kotlin
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// No need to call findViewById(R.id.textView) as TextView
textView.text = "Kotlin for Android rocks!"
}
}
4. 扩展功能,却不需用继承
扩展函数和属性让您轻松扩展类的功能而不用继承它们。提高代码可读性和自然性。
// Extend ViewGroup class with inflate function
fun ViewGroup.inflate(layoutRes: Int): View {
return LayoutInflater.from(context).inflate(layoutRes, this, false)
}
==================================
//Kotlin
// Call inflate directly on the ViewGroup instance
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = parent.inflate(R.layout.view_item)
return ViewHolder(v)
}
5. 100%兼容Java
Kotlin是一种JVM语言,可以与Java完全互操作。
//Kotlin
// Calling Java code from Kotlin
class KotlinClass {
fun kotlinDoSomething() {
val javaClass = JavaClass()
javaClass.javaDoSomething()
println(JavaClass().prop)
}
}
==================================
// Calling Kotlin code from Java
public class JavaClass {
public String getProp() { return "Hello"; }
public void javaDoSomething() {
new KotlinClass().kotlinDoSomething();
}
}
三、Great Tooling Support
Android Studio 3.0 provides helpful tools to help you start using Kotlin. Convert entire Java files or convert code snippets on the fly when you paste Java code into a Kotlin file.
Android Studio 3.0提供了有用的工具来帮助您开始使用Kotlin。 将Java代码粘贴到Kotlin文件中时,可以随时转换整个Java文件或转换代码片段。

四、Kotlin is Open
Just like Android, Kotlin is an open source project under Apache 2.0. Our choice of Kotlin reaffirms our commitment to an open developer ecosystem as we evolve and grow the Android platform, and we are excited to see the language evolve.
Android和Kotlin都是Apache 2.0下的一个开源项目。 我们(Google)对Kotlin的选择再次肯定了我们(Google)在开发和发展Android平台的过程中对开放式开发者生态系统的承诺,我们(Google)很高兴看到这门语言的发展。

网友评论