Kotlin学习笔记(三十七)数据类

/**
 * 数据类
 * 1.为类加上data关键词之后,该类变成数据类,
 * 会自动为属性添加getter和setter方法,以及copy、toString、hashCode、equals方法,
 * 也有对应的componentN属性,对应的是构造器里面的第N个参数
 * 2.为类重写componentN的方法之后,调用的时候可以使用()符号带参数来表示该对象
 * 3.默认的数据类是final类型的,同时没有无参的构造方法,需加noarg和allopen插件,
 * 就可以生成适当的javaBean
 */
class Country(val id: Int, val name: String) //国家类

@PoKo
data class Country2(val id: Int, val name: String) //国家类2

class ComponentX { //该类重写了component1到component4的方法
    operator fun component1(): String {
        return "斜阳"
    }

    operator fun component2(): String {
        return "空调"
    }

    operator fun component3(): String {
        return "WIFI"
    }

    operator fun component4(): String {
        return "西瓜"
    }
}

fun main(args: Array) {
    val china = Country(0, "中国")
    println(china)
    println(china.id)
    println(china.name)

    val england = Country2(1, "英国")
    println(england)
    println(england.component1()) //component1相当于于第一个参数id
    println(england.component2()) //component2相当于第二个参数name

    //此处的id,name分别对应Country2类中的component1和component2
    val (id, name) = england
    println(id)
    println(name)

    val componentX = ComponentX()
    val (a,b,c,d) = componentX //用()符号加参数承接对象的值
    println("$a $b $c $d")
}
Kotlin学习笔记(三十七)数据类_第1张图片
运行结果

此时在字节码生成的Country2类是final类,并且没有无参的构造方法。如果需要当作JavaBean使用,需要添加noarg和allopen插件。步骤如下:

1.新建PoKo.kt文件,声明注解其中代码如下:

annotation class PoKo

2.在build.gradle文件下添加:

apply plugin: 'kotlin-noarg'
apply plugin: 'kotlin-allopen'

noArg {
    annotation("com.kotlin.面向对象.annotations.PoKo")
}

allOpen {
    annotation("com.kotlin.面向对象.annotations.PoKo")
}

3.在build.gradle文件的dependencies中添加如下两行:

classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"

4.在对应的data class数据类添加Poko的注解即可。

@PoKo
data class Country2(val id: Int, val name: String) //国家类2

你可能感兴趣的:(Kotlin学习笔记(三十七)数据类)