Kotlin from learning to Android Chapter 2 idiomatic usage

Kotlin from learning to Android Chapter 2 idiomatic usage

1. Data class

Equivalent to beans in java, use the keyword data to declare this class;

data class User(val name: String, val age: Int)
var user:User = User("admin" , 20)

Due to the comparison of templates (such as: set, get, etc.), the following methods in Kotlin are automatically generated by the system:

  • equals()/hashCode()
  • toString() of the form “User(name=John, age=42)”,
  • .Property name (eg: user.age)
  • copy()

If these methods are overridden in the class, or the class inherited by the class has these methods, the system will not generate these methods again.

In order to ensure the correctness of the automatically generated code of the data class, the data class must meet the following conditions:

  • The main constructor has at least one parameter;
  • All primary constructor parameters must be declared with val or var;
  • Data classes cannot be modified with abstract, open, sealed or inner;
  • (versions before 1.1) data classes can only implement interfaces;
  • Starting from 1.1, data classes can inherit from other classes;

If a data class wants to use a parameterless constructor, its parameterized constructor must specify an initial value.

fun main(args: Array<String>) {
    var user:User = User()
    println(user.name)      // admin
}

data class User(val name: String = "admin", val age: Int? = 21)

The use of copy() method
In many cases, we need to copy a class, and then modify the value of one or several attributes, while the values ​​of other attributes remain unchanged, then we can use copy().

val jack = User(name = "Jack", age = 1)
val olderJack = jack.copy(age = 2)
println(olderJack.toString())           // User(name=Jack, age=2)

Data class and structure declaration

val jane = User("Jane", 35) 
val (name, age) = jane
println("$name, $age years of age") // Jane, 35 years of age

2. Create DTOs (POJOs/POCOs)

data class Customer(val name: String, val email: String)

3. Function parameter default value

fun foo(a: Int = 0, b: String = "") { ... }

4. Filter list

val positives = list.filter { x -> x > 0 }

or more simply

val positives = list.filter { it > 0 }

5. String insertion

println("Name $name")

6. Instance check

when (x) {
    is Foo -> ...
    is Bar -> ...
    else   -> ...
}

7. Traverse map/list

for ((k, v) in map) {
    println("$k -> $v")
}
// k , v 名字随意起,符合 Kotlin 命名规范即可

8. Scope of use

for (i in 1..100) { ... }  // 闭合范围包括 100
for (i in 1 until 100) { ... } // 半开放范围不包括 100
for (x in 2..10 step 2) { ... }
for (x in 10 downTo 1) { ... }
if (x in 1..10) { ... }

9. Read-only lists

val list = listOf("a", "b", "c")

10. Read-only map

val map = mapOf("a" to 1, "b" to 2, "c" to 3)

11. Map access

println(map["key"])
map["key"] = value

12. Laziness

val p: String by lazy {
    // compute the string
}

13. Extension function

fun String.spaceToCamelCase() { ... }

"Convert this to camelcase".spaceToCamelCase()

14. Create a singleton

object Resource {
    val name = "Name"
}

15.If not null shorthand

val files = File("Test").listFiles()

println(files?.size)

16.If not null and else shorthand

val files = File("Test").listFiles()

println(files?.size ?: "empty")

17. Execute the statement when the data is null

val data = ...
val email = data["email"] ?: throw IllegalStateException("Email is missing!")

18. Execute the statement when the data is not null

val data = ...

data?.let {
    ... // execute this block if not null
}

19. Use when to return function return value

fun transform(color: String): Int {
    return when (color) {
        "Red" -> 0
        "Green" -> 1
        "Blue" -> 2
        else -> throw IllegalArgumentException("Invalid color param value")
    }
}

20.try/catch

fun test() {
    val result = try {
        count()
    } catch (e: ArithmeticException) {
        throw IllegalStateException(e)
    }

    // Working with result
}

21. if expression

fun foo(param: Int) {
    val result = if (param == 1) {
        "one"
    } else if (param == 2) {
        "two"
    } else {
        "three"
    }
}

22. Use of Builder style functions

fun arrayOfMinusOnes(size: Int): IntArray {
    return IntArray(size).apply { fill(-1) }
}

23. Use the with keyword to call multiple methods in one instance at a time

class Turtle {
    fun penDown()
    fun penUp()
    fun turn(degrees: Double)
    fun forward(pixels: Double)
}

val myTurtle = Turtle()
with(myTurtle) { 
    penDown()
    for(i in 1..4) {
        forward(100.0)
        turn(90.0)
    }
    penUp()
}

24. Java 7 uses resources

val stream = Files.newInputStream(Paths.get("/some/file.txt"))
stream.buffered().reader().use { reader ->
    println(reader.readText())
}

25. Use of generics

//  public final class Gson {
//      ...
//     public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
//     ...

inline fun <reified T: Any> Gson.fromJson(json): T = this.fromJson(json, T::class.java)

26. Declare a nullable Boolean type data

val b: Boolean? = ...
if (b == true) {
    ...
} else {
    // b 是 false 或者 null 进入此逻辑
}

Supongo que te gusta

Origin blog.csdn.net/niuzhucedenglu/article/details/72812074
Recomendado
Clasificación