Kotlin artistic exploration process control and operator

If the use of

Using if-else expression, the most common written as


fun ifExpression(): Int{
    //1.最普通的写法
    var max: Int
    if (a < b) {
        max = b
    }
    else{
        max = a
    }
    return max
}
复制代码

Kotlin chestnuts in the above can be written as if the expression

val max = if(a > b) a else b
复制代码

Note: Be sure to complete the expression, can not be omitted else part

If there are multiple statements if the expression under certain conditions, then the last line is the result of its return


    val max2 = if(a > b){
        println("a > b")
        a
    }else{
        println("a < b")
        b
    }
复制代码

when expression

java and when the switch function is the same, the switch can be achieved, when can achieve, and to write more concise

General wording when expressions give chestnuts

fun whenExpression(x: Int) {
    when(x){
      1 -> println("x == 1")
      2 -> println("x == 2")
        else -> {
            print("x is neither 1 nor 2")
        }
    }
}
复制代码

Statement under when, if one qualifies, it will jump out and return the result directly

If there is a plurality of case, then the same approach, may be connected by commas, such as this:

  when(x){
        1, 2 -> println("x == 1 or x == 2")
        else ->  println("x is neither 1 nor 2")
    }
复制代码

If the case that the same approach it in a certain range? It can be written:

 when(x){
        in 1..10 -> println("x在1到10的范围内")
        !in 10..20 -> println("x不在10到20的范围内")
        else -> println("none of the above")
    }
复制代码

If that case is an expression, you can write:

 when(x){
        parseInt(s) -> println("s encodes x")
        else -> println("s does not encode x")
    }
复制代码

for loop

Number in the range of for loop iterates

    for(i in 1..10) println(i)
复制代码

downTo reduced from 6 0 step 2 to step

    for(i in 6 downTo 0 step 2) println(i)
复制代码

for looping through an array

   var array: Array<String> = arrayOf("I","am","jason","king")
    //迭代一个数组
    for(i in array.indices){
        print(array[i])
    }
复制代码

Through the array, the target value corresponding to the output

    for((index,value) in array.withIndex()){
        println("该数组下标$index 的元素是这个$value")
    }
复制代码

while loop

While loop while there are two ways, and do-while

fun whileLoop() {
    var x = 5
    //while
    while (x > 0){
        x--
        print("$x-->")
    }

    x = 5
    println()
    do {
        x--
        print("$x-->")
    } while (x >= 0) // y is visible here!
    
}
复制代码

Skip break out of the loop and continue cycling and other language grammar exactly the same, not go into here.

Details of the flow control section is transmitted to herein

Control Flow details of process control section can be transferred to the official documentation Control Flow

Operator overloading

Any class can be defined Kotlin or override the superclass basic operators, we need to use the keyword operator

For chestnut, of Complex class overloads the + operator

class Complex(var real: Double,var imaginary: Double){
    //重载加法运算符
    operator fun plus(other: Complex): Complex {
        return Complex(real + other.real,imaginary + other.imaginary)
    }
    
    //重写toString输出
    override fun toString(): String {
        return "$real + $imaginary"
    }
}
复制代码

In the main function using overloaded + operator

var c1 = Complex(1.0,2.0)
var c2 = Complex(2.0,3.0)
println(c1 + c2)
复制代码

Output

3.0 + 5.0

Details of the operator section can be transferred to official documents

Operator overloading

Guess you like

Origin blog.csdn.net/weixin_33885676/article/details/91388200