0
点赞
收藏
分享

微信扫一扫

kotlin 学习,val和var的区别


学习:

​​https://www.runoob.com/kotlin/kotlin-tutorial.html​​

​​https://www.jianshu.com/p/f9e78d6c54bd​​

练习:

​​https://www.runoob.com/try/runcode.php?filename=hello&type=kotlin​​

​​https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20version/Simplest%20version.kt​​       这个好用

 

var是一个可变变量,这是一个可以通过重新分配来更改为另一个值的变量。这种声明变量的方式和Java中声明变量的方式一样。
具体使用

val是一个只读变量,这种声明变量的方式相当于java中的final变量。一个val创建的时候必须初始化,因为以后不能被改变。

 

sample:

/**
* We declare a package-level function main which returns Unit and takes
* an Array of strings as a parameter. Note that semicolons are optional.
*/

package hello // 可选的包头



class DB(){
fun get(){
println("aa")

}}
fun sum(a:Int,b:Int): Int{
return a*b
}
fun sum2(a:Int,b:Int): Int=a+b
fun main(args: Array<String>)
{
val aa=sum(2,3)
println(aa)
println(sum2(2,4))

}

 

举报

相关推荐

0 条评论