Kotlin学习笔记(二)——示例篇三

来源:互联网 发布:python blog 教程 编辑:程序博客网 时间:2024/05/19 02:30

该篇继续根据Kotlin语言的Demo,来学习Kotlin的使用!本文继上一篇文章来继续学习的,如果有些不懂,请参考上一篇文章
参考链接地址:http://try.kotlinlang.org/#/Examples/Multi-declarations%20and%20Data%20classes/Multi-declarations/Multi-declarations.kt

示例一

/** * This example introduces a concept that we call mutli-declarations. * It creates multiple variable at once. Anything can be on the right-hand * side of a mutli-declaration, as long as the required number of component * functions can be called on it. * See http://kotlinlang.org/docs/reference/multi-declarations.html#multi-declarations */fun main(args: Array<String>) {    val pair = Pair(1, "one")    val (num, name) = pair    println("num = $num, name = $name")}class Pair<K, V>(val first: K, val second: V) {    operator fun component1(): K {        return first    }    operator fun component2(): V {        return second    }}

分析:上述代码中定义了一个类对象,关于如何建一个类和类的构造函数,请参考Data class文章。
另一个问题是关于代码val (num, name) = pair的使用,这么写代码表示什么意思呢?
根据参考文档http://kotlinlang.org/docs/reference/multi-declarations.html#example-returning-two-values-from-a-function,
看看我们需要从一个函数方法返回两个值,来看例子:

/***数据类对象*/data class Result(val result: Int, val status: Status)fun function(...): Result {    // computations    return Result(result, status)}// Now, to use this function:val (result, status) = function(...)

由于数据类对象自动生成componentN()函数,所以这里并没有进行生成!

NOTE: we could also use the standard class Pair and have function() return Pair

示例二:

/** *  Data class gets component functions, one for each property declared *  in the primary constructor, generated automatically, same for all the *  other goodies common for data: toString(), equals(), hashCode() and copy(). *  See http://kotlinlang.org/docs/reference/data-classes.html#data-classes */data class User(val name: String, val id: Int)fun getUser(): User {    return User("Alex", 1)}fun main(args: Array<String>) {    val user = getUser()    println("name = ${user.name}, id = ${user.id}")    // or    val (name, id) = getUser()    println("name = $name, id = $id")    // or    println("name = ${getUser().component1()}, id = ${getUser().component2()}")}

分析: 每一个数据类对象,都会自动生成与其属性相对应的componnent方法。

示例三:

/** *  Kotlin Standart Library provide component functions for Map.Entry */fun main(args: Array<String>) {    val map = hashMapOf<String, Int>()    map.put("one", 1)    map.put("two", 2)    for ((key, value) in map) {        println("key = $key, value = $value")    }}

分析:该代码创建了一个Map集合,并将Map集合的值打印出来!创建HashMap直接调用hashMapOf方法即可。遍历的时候使用 in 语法来遍历!(key,value)这只是一种形式,字符不限制于key和value,可以是(k,v)

示例四:

/** * Data class gets next functions, generated automatically: * component functions, toString(), equals(), hashCode() and copy(). * See http://kotlinlang.org/docs/reference/data-classes.html#data-classes */data class User(val name: String, val id: Int)fun main(args: Array<String>) {    val user = User("Alex", 1)    println(user) // toString()    val secondUser = User("Alex", 1)    val thirdUser = User("Max", 2)    println("user == secondUser: ${user == secondUser}")    println("user == thirdUser: ${user == thirdUser}")    // copy() function    println(user.copy())    println(user.copy("Max"))    println(user.copy(id = 2))    println(user.copy("Max", 2))}

分析:以上代码主要展现了copy的使用。copy用于复制对象,并在复制过程中可以改变对象的值!比如:user.copy(id=2)改变了user的id值!

0 0
原创粉丝点击