[toc]

preface

Let’s move on to the program structure, this part is relatively simple, sometimes can be more verbose, notes! It’s the problems I meet in the process of learning, as a record. It’s better if you don’t have a problem. Some are personal understanding, better understanding is welcome in the comments.

Operator overload (operator overload)

This section of the official website is easier to understand, and also the Chinese operator overloading suggestions to look at the example:

class Complex(var real: Double, var imaginary: Double) {

    operator fun plus(other: Complex): Complex {
        return Complex(real + other.real, imaginary + other.imaginary)
    }

    operator fun plus(other: Int): Complex {
        return Complex(real + other, imaginary + other)
    }

    operator fun plus(other: Any): Int {
        return real.toInt()
    }

    override fun toString(): String {
        return "$real +${imaginary}i"
    }
}

class Book {
    infix fun on(any: Any): Boolean {
        return false} } class Desk fun main(args: Array<String>) {val c1 = Complex(2.0, 3.5) val c2 = Complex(4.0, 7.5) println(c1 + c2) println(c1 + 5) println(c1 + 5)"HelloWorld")
    if ("a" inArgs) {// infix infix expressions should not be used to reduce readability}if(Book() on Desk()) {Copy the code

Conclusion:

  • Operator overloaded methods must be denoted as operator and this is operator method
  • Operator overloading can only be performed based on the existing method names on the official website, such as + plus-minus += plusAssign
  • Only the number of parameters is required, but the parameter type and return type are not required
Expressions (infix expressions, branch expressions, when expressions)
  • Infix expression

Methods with only one parameter can be used without objects. Method name called this way:

class Book {
    infix fun on(any: Any): Boolean {
        return false
    }
}
class Desk

fun main(args: Array<String>) {
    if(Book() on Desk()) {Copy the code

Infix expressions are commonly used in DSLS and are not recommended because they reduce readability

  • Branching expression

If statement:

fun main(args: Array<String>) {
    val mode = if (args.isNotEmpty() && args[0] == "1"{0})else{1}}Copy the code

Expressions are called expressions instead of conditional statements because Kotlin has a return value

  • When the expression

Function is similar to switch in Java, also with a return value

fun main(args: Array<String>) {
    val x = 5
    when (x) {
        !in1.. 100 -> println("$x is not in 100")
        in1.. 100 -> println("$x is in 100 ")
        args[0].toInt() -> println("x==args[0]")
        is Int -> println("Hello is $x")
        else -> println("default case"// return args[0].toint () == 5 -> 1else- > 2}}Copy the code

If the condition matches none of the following statements will be executed, else -> statements will be executed if none match

Default parameter Variable length parameter Named parameter
  • The default parameters

These attributes are very simple to use as examples:

fun hello(char: Char='A'){
    
}
Copy the code

A default parameter specifies a default value for the parameter, which can be called without passing a parameter. The default value is used when no transmission is performed

  • Variable-length argument

There are similar variable-length arguments in Java

Java
 public void vararg1(String... strings) {
}

Kotlin  
public void vararg1(vararg args:String) {
}
Copy the code

Variable-length arguments are similar to arrays. Traversal is the same as an array or collection. The last Kotlin parameter is not required, as in:

Java public void vararg1(String... Public void vararg2(String... Strings,int I) {Copy the code

In Kotlin there is no requirement because there are named parameters

  • A named parameter is passed with the name of the parameter, for example:
fun hello(double: Double, vararg args: Int, string: String) {args. ForEach (::println) println(String)} call: hello(3.0, 1, 2, 3, 4, String ="Hello")// No named parameter is required before the variable length parameterCopy the code
  • Array expansion (Spread Operator)
    val array = intArrayOf(1, 2, 3, 4)
    val list = ArrayList<Int>()
    list.add(1)
    list.add(2)
    list.add(3)
    hello(*array, string = "hello") Hello (3.0, *array, string ="hello")
    hello(args = *array, string = "hello"// Hello (*list,string ="hello"//list does not currently support * representationCopy the code
The - * operator can expand arrays, but collections are not yet supported. - * The operator is only used for variable-length argument lists. - * the operator cannot be overloadedCopy the code

conclusion

That concludes this article, and the next one is about object-oriented notes

Github source directly run, including all detailed notes