Kotlin基本语法
来源:互联网 发布:发票打印软件 编辑:程序博客网 时间:2024/05/11 14:09
1.包声明
package com.sample.hellokotlin与java一样只是没有了分号
2.函数声明
// 关键字 fun ,Int 类型 fun sum(a: Int, b: Int): Int { return a + b; }
fun sum(a: Int, b: Int) = a + b //直接返回 返回推断的类型
//Unit 无返回值 fun sum2(a: Int, b: Int):Unit { } //上面Unit可以省略 fun sum2(a: Int, b: Int) { }
fun sum2(a: Int, b: Int):Unit { //打印函数 println("$a $b ,${a + b}") }
3.变量声明
//使用 var 关健字 声明表示这个变量是可变的 var addr: String = ""; //使用 val 关健字 声明表示这个变量是不可变的 val lon: String = "";
“`
//类声明
class Addr {
var addr: String = "";val lon: String = "";var lat: String = "";fun copyAddr(): Addr { //相当于 new Addr();在Kotlin中没有 new 关健字 val result = Addr(2) //直接赋值 result.addr = "深圳" return result}//如果这样写必须在构造函数中初始化val simple: Int?//构造函数constructor(simple: Int?) { this.simple = simple}//推断类型为 Intval inferredType = 1/*一个变量的声明完整语法 var <propertyName>[: <PropertyType>] [= <property_initializer>] [<getter>] [<setter>] *///默认get set 方法var game: String = "555"val isEmpty: Boolean get() = this.size == 0
阅读全文
0 0
- Kotlin 基本语法
- Kotlin基本语法
- kotlin基本语法
- kotlin基本语法
- Kotlin基本语法
- Kotlin基本语法
- Kotlin 基本语法 (一)
- Kotlin 基本语法 (二)
- Kotlin~基本语法
- KOtlin 基本语法
- kotlin---基本语法
- Kotlin基本语法
- Kotlin 基本语法
- Kotlin基本语法
- Kotlin基本语法
- Kotlin基本语法
- Kotlin基本语法
- Kotlin基本语法
- 面试常见排序算法(中)
- angularjs学习笔记一——了解angularjs、开发环境搭建、第一个angularjs程序
- IEEE参考文献格式
- iframe文件上传
- STM32系统滴答_及不可不知的延时技巧
- Kotlin基本语法
- 基于OpenCV和VTK的冠脉三维重建
- 网络编程基础
- Oracle ADF 报错:JBO-33001
- LeetCode 112. Path Sum Add to List
- Spark源码系列(二)RDD详解
- source insight的快捷键总结
- PDF文件字体嵌入
- c++计算程序运行时间