kotlin解构
kotlin解构指的是将对象的多个属性分别赋值给多个变量的过程
class Student(val id: Int, val name: String, val sex: Char) {
//其中的固定格式为 operator fun component[num],其中component为固定写法不能改变
operator fun component1(): Int = id
operator fun component2(): String = name
operator fun component3(): Char = sex
operator fun component4(): String = "111"
}
fun main() {
val s = Student(1, "student", 'M')
val (n1, n2, n3, n4) = s
//id
println(n1)
//name
println(n2)
//
kotlin解构最先出现在Python成神之路。
共有 0 条评论