fun main(args : Array<String>){ println(student_data("Joe", 27)) println(student_data("Jane", 30)) } //占位符 字符串模板 """模板内容""" 使用 """ 三个引号括起来, ${表达式} 是用来替换的字符串内容 fun student_data(name : String, age : Int)
fun main(args: Array<string>) { // code here } The main function must be present in each Kotlin program. It accepts an array of strings as parameter and returns nothing. If a function returns nothing, the return type can be omitted. In such cases the type inferred is Unit. This is ...
扩展运算符是一种将数组或集合打散为参数列表的方式,例如: funsum(a:Int,b:Int,c:Int)=a+b+cvallist=listOf(1,2,3)valresult=sum(*list.toIntArray()) 函数/属性的引用 支持属性引用,可以使用::运算符来引用属性 支持函数引用,可以使用::运算符来引用函数 funtest8(){classPerson(valname:String){fu...
interface Runnable { fun run() } @JvmStatic fun main(args: Array<String>) { // 创建Runnable对象并使用匿名内部类重写run方法 val runnable: Runnable = object : Runnable { override fun run() { println("Runnable is running") } } // 创建Thread对象并将Runnable作为参数传入 val thread: Thread ...
fun main(args: Array<String>) { val fruit:String= "orange"//1 a = "banana" //2 Error } 创建水果变量,并用字符串orange的值进行初始化 编译器会抛出错误,因为水果变量已经被初始化 Kotlin 还允许我们在文件级别定义变量和函数。我们将在第三章“玩转函数”中进一步讨论。
for (index in array.indices) { } 反编译后: for(int var4 = array.length; var3 < var4; ++var3) { } 通过indices 遍历数组, 编译之后的代码 ,除了创建了一些临时变量,并没有创建额外的对象。 通过withIndex 遍历数组 withIndex 和indices 遍历数组的方式相似,通过 withIndex 遍历数组,不仅可以获取的...
// 数据类,增加了 agedata class People( val name: String, val age: Int, val city: String)fun main(args: Array<String>) { // 编译测试 printlnPeople(People("dhl", 80, "beijing"))}复制代码 1. 此时没有更改解构声明,也不会有任何错误,编译输出结果如下所示: ...
interface Runnable { fun run() } @JvmStatic fun main(args: Array<String>) { // 创建Runnable对象并使用匿名内部类重写run方法 val runnable: Runnable = object : Runnable { override fun run() { println("Runnable is running") } } // 创建Thread对象并将Runnable作为参数传入 val thread: Thread ...
It may be possible to reduce the footprint using phantom types or some form of union type bound (cf. Kotlin, Java). When the shape of an N-dimensional array is known at compile-time, we can use type-level integers to ensure shape conforming tensor operations (inspired by Nexus and ...
fun main(args: Array<String>){ val num = 100 } 1. 2. 3. 4. 5. 6. 7. 8. 9. 2. 浮点类型 Kotlin 对于小数的默认推断是Double类型。如果需要显式将一个小数指定为Float类型需要在数值尾部加入f或F。由于Float类型十进制位数是6位,所以例子中floatNumber实际值大小为3.1415926,后面就会出现进度丢失舍...