1. class MyActivity : AppCompatActivity() {
    2. override fun onCreate(savedInstanceState: Bundle?) {
    3. super.onCreate(savedInstanceState)
    4. setContentView(R.layout.activity)
    5. }
    6. }

    https://developer.android.com/kotlin/learn

    Lambda 表达式,其实就是匿名函数。而函数其实就是功能(function),匿名函数,就是匿名的功能代码了。

    1. 每个条件分支都隐式地返回其最后一行上的表达式的结果,因此您无需使用 return 关键字。
    2. val answerString: String = if (count == 42) {
    3. "I have the answer."
    4. } else if (count > 35) {
    5. "The answer is close."
    6. } else {
    7. "The answer eludes me."
    8. }
    9. println(answerString)
    10. //还可以这样
    11. val answerString = when {
    12. count == 42 -> "I have the answer."
    13. count > 35 -> "The answer is close."
    14. else -> "The answer eludes me."
    15. }
    16. println(answerString)
    1. //函数
    2. fun generateAnswerString(countThreshold: Int): String {
    3. val answerString = if (count > countThreshold) {
    4. "I have the answer."
    5. } else {
    6. "The answer eludes me."
    7. }
    8. return answerString
    9. }
    10. //Kotlin 支持局部函数,即一个函数在另一个函数内部:
    11. //泛型函数