美文网首页
每天学一点 Kotlin -- 多彩的类:数据类

每天学一点 Kotlin -- 多彩的类:数据类

作者: 冯可乐同学 | 来源:发表于2021-11-19 16:38 被阅读0次

----《第一季Kotlin崛起:次世代Android开发 》学习笔记

总目录:每天学一点 Kotlin ---- 目录
上一篇:每天学一点 Kotlin -- 类的进阶:扩展
下一篇:每天学一点 Kotlin -- 多彩的类:密封类

1. 数据类

1.1 在 Kotlin 中,有时候仅仅只需要一些数据而不需要调用方法,那么会将这些数据单独放在一个类里,我们就称这个类是数据类。---- 简单粗暴理解:只有属性,没有方法(类比于 Java 中的 javabean)。

1.2 在 Kotlin 中,数据类的声明用关键字 data 作为标记。举个栗子,声明一个树叶的数据类:

data class Leaf(val size: String, val color: String, val shap: String, val vain: Int)

1.3 从上面的例子说一下数据类的声明条件:
(1) 最简单的是主构造函数,最少要有一个参数;
(2) 数据类的主构造器的所有参数必须标记为 val 或 var;
(3) 数据类不能是抽象类,open 类,封闭(sealed)类, 或内部类(inner);
(4) 数据类也不能继承自任何其他类。
(5) 数据类可以实现接口

2. 数据类的使用

2.1 访问数据类的2种方法:
(1) 和之前普通类一样:对象名.属性名
(2) 编译器从主构造函数中声明的属性中导出的成员方法 componN() 函数群。
举个栗子:

data class Leaf(val size: String, val color: String, val shape: String, val vein: Int)

fun testLeaf01(){
    val myLeaf = Leaf("30", "green", "circle", 56)
    // 第一种方式
    val mySize = myLeaf.size
    val myColor = myLeaf.color
    val myShape = myLeaf.shape
    val myVein = myLeaf.vein

    println("mySize = $mySize, myColor = $myColor, myShape = $myShape, meVein = $myVein")
}

fun testLeaf02(){
    val myLeaf = Leaf("30", "green", "circle", 56)

    // 第二种方式:用到了 componentN 函数群
    val(size1, color1, shape1, vein1) = myLeaf
    println("size1 = $size1, color1 = $color1, shape1 = $shape1, vein1 = $vein1")
}

fun main(args: Array<String>) {
    testLeaf01()
    testLeaf02()
}

打印结果:

mySize = 30, myColor = green, myShape = circle, meVein = 56
size1 = 30, color1 = green, shape1 = circle, vein1 = 56

2.2 解构:顾名思义就是把一个对象分解成多个变量。就像上面代码中的第二种方式。但是有一点要注意:就是解构时变量的顺序要和主构造函数参数的顺序一致。

2.3 componentN 函数群会按数据体重的声明顺序对应于所有属性进行赋值。举个栗子:

fun testLeaf03() {
    val myLeaf = Leaf("30", "green", "circle", 56)
    val size = myLeaf.component1()
    val color = myLeaf.component2()
    val shape = myLeaf.component3()
    val vein = myLeaf.component4()
    println("size3 = $size, color3 = $color, shape3 = $shape, vein3 = $vein")
}

fun main(args: Array<String>) {
    testLeaf03()
}

打印结果:

size3 = 30, color3 = green, shape3 = circle, vein3 = 56

3. 复制对象

3.1 copy() 函数:如果一个数据类里面数据很多,那在定义一个对象并赋值就会很麻烦。还好编译器提供了一个复制函数 copy() ,它能一个对象,改变它的一些属性,但是其余部分保持不变,以达到符合新的要求。

3.2 举个栗子:对上面的树叶的对象进行复制:

fun testLeaf04() {
    val myLeaf1 = Leaf("30", "green", "circle", 56)
    val myLeaf2 = myLeaf1.copy(size = "40")
    val myLeaf3 = myLeaf1.copy(color = "yellow")
    val myLeaf4 = myLeaf1.copy(vein = 40)
    println("myLeaf1: $myLeaf1")
    println("myLeaf2: $myLeaf2")
    println("myLeaf3: $myLeaf3")
    println("myLeaf4: $myLeaf4")
}

fun main(args: Array<String>) {
    testLeaf04()
}

打印结果:

myLeaf1: Leaf(size=30, color=green, shape=circle, vein=56)
myLeaf2: Leaf(size=40, color=green, shape=circle, vein=56)
myLeaf3: Leaf(size=30, color=yellow, shape=circle, vein=56)
myLeaf4: Leaf(size=30, color=green, shape=circle, vein=40)

可以看到,复制对象并且改变了部分属性之后,对原来的对象并不会产生影响。

4. toString()

4.1 toString() 函数:就跟 Java 中的 toString() 函数是一样的,区别就是 Kotlin 中数据类的 toString() 函数是编译器自带的。举个栗子:

fun testLeaf05(){
    val myLeaf = Leaf("30", "green", "circle", 56)
    println("myLeaf = ${myLeaf.toString()}")
}
fun main(args: Array<String>) {
    testLeaf05()
}

打印结果:

myLeaf = Leaf(size=30, color=green, shape=circle, vein=56)

4.2 如果想要修改 toString() 函数返回的字符串的格式,只需覆盖重写就可以了。

data class Leaf2(val size: String, val color: String, val shape: String, val vein: Int) {
    override fun toString(): String {
        var result = "size:${size} -- color:${color} -- shape:${shape} -- vein:${vein}"
        return result
    }
}

fun testLeaf06() {
    val myLeaf2 = Leaf2("30", "green", "circle", 56)
    println("myLeaf2 = ${myLeaf2.toString()}")
}

fun main(args: Array<String>) {
//    testLeaf01()
//    testLeaf02()
//    testLeaf03()
//    testLeaf04()
//    testLeaf05()
    testLeaf06()
}

打印结果:

myLeaf2 = size:30 -- color:green -- shape:circle -- vein:56
相关代码:https://gitee.com/fzq.com/test-demo

相关文章

网友评论

      本文标题:每天学一点 Kotlin -- 多彩的类:数据类

      本文链接:https://www.haomeiwen.com/subject/skytaltx.html