Kotlin 数据类
我们经常创建类来保存一些数据。在这样的类中,一些标准函数通常可以从数据中导出。在 Kotlin 中,这种类型的类称为数据类并标记为data 。
数据示例:
data class Student(val name: String, val roll_no: Int)
编译器自动派生以下函数:
- 等于()
- 哈希码()
- toString()
- 复制()
创建数据类的规则——
数据类必须满足以下要求以确保一致性:
- 主构造函数至少需要一个参数。
- 所有主要的构造函数参数都需要标记为val或var 。
- 数据类不能是抽象的、开放的、密封的或内部的。
- 数据类只能实现接口。
toString()
此函数返回数据类中定义的所有参数的字符串。
例子:
fun main(args: Array)
{
//declaring a data class
data class man(val roll: Int,val name: String,val height:Int)
//declaring a variable of the above data class
//and initializing values to all parameters
val man1=man(1,"man",50)
//printing all the details of the data class
println(man1.toString());
}
输出:
man(roll=1, name=man, height=50)
笔记:
编译器仅将主构造函数中定义的属性用于自动生成的函数。
它不包括在类主体中声明的属性。
例子:
fun main(args: Array)
{
//declaring a data class
data class man(val name: String)
{
//property declared in class body
var height: Int = 0;
}
//declaring a variable of the above data class and
//initializing values to all parameters
val man1=man("manish")
//class body properties must be assigned uniquely
man1.height = 70
//this method prints the details of class that
//are declared in primary constructor
println(man1.toString());
//printing the height of man1
println(man1.height);
}
输出:
man(name=manish)
70
这里的高度不是由 toString()函数使用的。
复制()
有时我们需要复制一个对象,在它的某些属性中进行一些更改,保持所有其他属性不变。
在这种情况下,使用了copy ()函数。
复制()的属性
- 它复制主构造函数中定义的所有参数或成员
- 如果定义,两个对象可以具有相同的主参数值和不同的类主体值
副本声明()
fun copy(name: String = this.x, age: Int = this.y) = user(x, y)
其中user是一个数据类: user(String, Int) 。
例子
fun main(args: Array)
{
//declaring a data class
data class man(val name: String, val age: Int)
{
//property declared in class body
var height: Int = 0;
}
val man1 = man("manish",18)
//copying details of man1 with change in name of man
val man2 = man1.copy(name="rahul")
//copying all details of man1 to man3
val man3 = man1.copy();
//declaring heights of individual men
man1.height=100
man2.height=90
man3.height=110
//man1 & man3 have different class body values,
//but same parameter values
//printing info all 3 men
println("${man1} has ${man1.height} cm height")
println("${man2} has ${man2.height} cm height")
println("${man3} has ${man3.height} cm height")
}
输出:
man(name=manish, age=18) has 100 cm height
man(name=rahul, age=18) has 90 cm height
man(name=manish, age=18) has 110 cm height
hashCode() 和 equals()
- hashCode ()函数返回对象的哈希码值。
- 如果两个对象具有相同的内容, equals () 方法返回 true,它的工作方式类似于“==”,但对于Float和Double值的工作方式不同。
hashCode() 声明:
open fun hashCode(): Int
hashCode() 的属性
- 在同一对象上两次声明的两个哈希码将相等。
- 如果两个对象根据equals ()方法相等,那么哈希码
返回也将相同
fun main(args: Array)
{
//declaring a data class
data class man(val name: String, val age: Int)
val man1 = man("manish",18)
val man2 = man1.copy(name="rahul")
val man3 = man1.copy();
val hash1=man1.hashCode();
val hash2=man2.hashCode();
val hash3=man3.hashCode();
println(hash1)
println(hash2)
println(hash3)
//checking equality of these hash codes
println("hash1 == hash 2 ${hash1.equals(hash2)}")
println("hash2 == hash 3 ${hash2.equals(hash3)}")
println("hash1 == hash 3 ${hash1.equals(hash3)}")
}
输出:
835510190
-938448478
835510190
hash1 == hash 2 false
hash2 == hash 3 false
hash1 == hash 3 true
解释:
man1和man2具有相同的对象内容,因此它们相等,因此它们具有相同的哈希码值。
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。