当前位置:网站首页>Kotlin learning 1: variables, functions, conditional statements and loop statements
Kotlin learning 1: variables, functions, conditional statements and loop statements
2022-07-22 20:06:00 【Wu Qingsen】
For a long time Android Development , see kotlin Code , Probably understand , Look carefully but don't understand . So learn about it systematically , Enter a door .
Create the first project
Created the first kotlin project , It only needs java Change it to kotlin that will do :
Compiler error :
Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72.
...
Modify and delete jcenter() :
repositories {
mavenCentral()
google()
}
Can run .
Variable
val : Immutable variable , Corresponding Java Of final .
var : Volatile variables , Corresponding Java Non - final.
Define a variable , No semicolon at the end :
val a = 10
or
val a: Int = 10
Java How to write it :
private int a = 10;
It's really simpler . it is to be noted that Java Medium int, stay kotlin Turn into Int, All initials of other data types are capitalized ;
take a Do calculations , Now a Use var Statement :
var a = 10
a = a * 5
stay kotlin in ,val and var It must be added , This is different from Java,Java Of final Is additive or non additive .
When using , Always give priority to val, When val Not enough to use var.
function
That is, the method often said . Define one pass in two parameters , And the return value is int Function of type :
fun setName(val1: Int, val2: String): Int {
return 100;
}
fun Fix the writing method for the function ,setName Is the function name ; The first parameter is int type , Parameter name val1, The second is String type , Parameter name val2; Change the function to return a int Type value .
Conditional statements :if and when
if sentence , And Java It can't be as like as two peas. , It can only be said that there is no difference :
val a = 10
val b = 20
if (a > b) {
Log.w(" Maximum a", "" + a)
} else {
Log.w(" Maximum b", "" + b)
}
When there are many conditional statements , have access to when, Call method transmission B You can get it 9527 了 :
fun getInt(name: String):Int {
return when (name) {
"A" -> 9526
"B" -> 9527
"c" -> 9528
else -> 0
}
}
// The following is the abbreviation :
fun getInt(name: String) =
when (name) {
"A" -> 9526
"B" -> 9527
"c" -> 9528
else -> 0
}
Abbreviation can save a lot of code for a large amount of code .
The above code can also be written like this :
fun getInt(name: String) =
when {
name == "A" -> 9526
name == "B" -> 9527
name == "c" -> 9528
else -> 0
}
In this way, you can write some complex logic , such as :
fun getInt(name: String) =
when {
name.contains("A") -> 9526
name == "B" -> 9527
name == "c" -> 9528
else -> 0
}
The actual needs are much more complex than the above .
Loop statement while and for
do while and Java similar :
var b = 0
do {
b++
Log.w("b The value of is ", "" + b)
} while (b < 12)
for Recycling changes slightly ,for-in Cycle through each element of the interval and print :
for (i in 0..10){
Log.w("i The value of is ", "" + i)
}
A slightly more complex common usage , Data in the loop list :
var myList = arrayListOf<String>() // Define an array
myList.add("A")
myList.add("B")
myList.add("C")
myList.add("D")
for (i in 0..myList.size) {
Log.w(" The value in the list is ", "" + myList[i])
}
Combined with some keywords, you can realize a more complex cycle :
for (i in 0 until 10){
Log.w("i The value of is ", "" + i)
}
until It means that the right side is the open section ,i The range of values is 0-9, That is, mathematical expression :[0,10).
More complex cycles :
for (i in 0 until 10 step 2){
Log.w("i The value of is ", "" + i)
}
Print the results :
i The value of is : 0
i The value of is : 2
i The value of is : 4
i The value of is : 6
i The value of is : 8
step 2 It means i Increase by two each time , So with the print result above .
Take another look at the descending order ,downTo Representation of descending order :
for (i in 0 downTo -10 step 2){
Log.w("i The value of is ", "" + i)
}
First create a [0,-10] In descending order , And then let i Every reduction 2, The result is :
i The value of is : 0
i The value of is : -2
i The value of is : -4
i The value of is : -6
i The value of is : -8
i The value of is : -10
In this way, you can write some simple logic .
边栏推荐
- Pregel function in spark graphx (Reprint)
- 【数据库基础干货】MySQL基础及慢查询优化实践
- One master-slave replication of MySQL
- Spark RDD depends on the working principle of DAG
- Typora下载和简单使用教程
- AttributeError: module ‘tensorflow.keras.utils‘ has no attribute image_dataset_from_directory——解决方法
- Flutter 2进阶(九):FijkPlayer播放视频与卡片效果
- 数组push时 覆盖的问题
- spark Json日志分析
- MySQL master-slave synchronization problem, repair the slave Library (transfer)
猜你喜欢
随机推荐
Customization of development mode of NFT mining Dividend System
From data standards to database design: solve the last mile problem of basic data standards (Part 2)
AttributeError: module ‘tensorflow.keras.utils‘ has no attribute image_dataset_from_directory——解决方法
Tensor和NumPy相互转换「建议收藏」
Promise usage
plsql不能初始化
Flutter 2进阶(一):Flutter实用技巧
Xcode11 添加lanuchimage黑屏无法显示问题
小鸟平台隐私政策
Decimal operation in shell (BC)
Fluent 2 Advanced (III): imitate BiliBili, login and register
【读书笔记】《微习惯:瘦身篇》
Privacy policy of bird platform
Kotlin学习一:变量、函数、条件语句与循环语句
promise用法
Data model design of newsql database
抽象类,接口
var、let、const区别
梁宁产品课程学习
ES6用法