Kotlin 第三章
Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,被称之为 Android 世界的Swift,由 JetBrains 设计开发并开源。
Kotlin 可以编译成Java字节码,也可以编译成 JavaScript,方便在没有 JVM 的设备上运行。
在Google I/O 2017中,Google 宣布 Kotlin 成为 Android 官方开发语言。
Kotlin 的一些基本方法 以及函数
package com.example.test710
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
fun main(Args:Array<String>){
// println(sum(1,2));
// println(Maxof(300,100))
// println(maxof2(500,100))
// println(forin())
// println( When(1))
}
fun sum(a:Int,b:Int):Int{ //TODO 返回值为int类型的方法
return a+b;
}
fun String(){ //TODO 字符串$拼接
var a=1
var b="a is $a"
println(b)
}
fun Maxof(a:Int,b:Int):Int{ //TODO 条件表达式
if(a>b){
return a
}else {
return b
}
}
fun maxof2(a:Int,b:Int)=if(a>b)a else b //TODO if表达式
fun forin(){ //TODO for 循环
var items= listOf<String>("斯蒂芬库里","詹姆斯哈登","保罗乔治","德里克罗斯")
//TODO for in
// for(i in items){
// println(i)
// }
//TODO foreach
items.forEach {
println(it)
}
}
var call : callBack=object : callBack{
override fun show(a: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
interface callBack{
fun show(a:Int)
}
class Mycall: callBack{
override fun show(a: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
fun When(a:Int){ //TODO When 用法
when(a){
1 -> "是1"
2 -> "是2"
3 -> "是3"
else ->"是其他"
}
}
object Resource{ //TODO 创建单例
val name="Name"
}