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

该篇继续根据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值!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值