data
var/val
修饰toString
,hashCode
,equals
,其中equals不是比较两个对象的地址,而是比较两个对象的主构造函数中的属性值copy()
函数:需要复制一个类对象,但是改变它的某些属性,保持剩余其他的属性值不变。var ( age,name, sex) = dataClass2
println("name = $name,age=$age,Sex=$sex")
解构声明中参数的顺序和主构造函数中参数相同
package classes
import javafx.scene.text.FontWeight
data class DataClass(val name: String, val age: Int, val sex: String) {
var weight: Float = 0.0f
get() = field
set(value) {
field = value
}
constructor(weight: Float) : this(name = "Tom", age = 18, sex = "男") {
}
}
反编译成java类,注意观察toString
,hashCode
,equals
,验证7中总结的
``
package classes;
public final class DataClass {
private float weight;
@NotNull
private final String name;
private final int age;
@NotNull
private final String sex;
public final float getWeight() {
return this.weight;
}
public final void setWeight(float var1) {
this.weight = var1;
}
@NotNull
public final String getName() {
return this.name;
}
public final int getAge() {
return this.age;
}
@NotNull
public final String getSex() {
return this.sex;
}
public DataClass(@NotNull String name, int age, @NotNull String sex) {
Intrinsics.checkParameterIsNotNull(name, "name");
Intrinsics.checkParameterIsNotNull(sex, "sex");
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public DataClass(float weight) {
this("Tom", 18, "男");
}
@NotNull
public final String component1() {
return this.name;
}
public final int component2() {
return this.age;
}
@NotNull
public final String component3() {
return this.sex;
}
@NotNull
public final DataClass copy(@NotNull String name, int age, @NotNull String sex) {
Intrinsics.checkParameterIsNotNull(name, "name");
Intrinsics.checkParameterIsNotNull(sex, "sex");
return new DataClass(name, age, sex);
}
// $FF: synthetic method
// $FF: bridge method
@NotNull
public static DataClass copy$default(DataClass var0, String var1, int var2, String var3, int var4, Object var5) {
if ((var4 & 1) != 0) {
var1 = var0.name;
}
if ((var4 & 2) != 0) {
var2 = var0.age;
}
if ((var4 & 4) != 0) {
var3 = var0.sex;
}
return var0.copy(var1, var2, var3);
}
public String toString() {
return "DataClass(name=" + this.name + ", age=" + this.age + ", sex=" + this.sex + ")";
}
public int hashCode() {
return ((this.name != null ? this.name.hashCode() : 0) * 31 + this.age) * 31 + (this.sex != null ? this.sex.hashCode() : 0);
}
public boolean equals(Object var1) {
if (this != var1) {
if (var1 instanceof DataClass) {
DataClass var2 = (DataClass)var1;
if (Intrinsics.areEqual(this.name, var2.name) && this.age == var2.age && Intrinsics.areEqual(this.sex, var2.sex)) {
return true;
}
}
return false;
} else {
return true;
}
}
}
Main函数
package classes
fun main(args: Array) {
var dataClass = DataClass(140.00F)
dataClass.weight = 140F
println(dataClass.weight)
println(dataClass.toString())
var dataClass1 = DataClass(140.00F)
dataClass1.weight = 150F
println(dataClass1.weight)
println(dataClass1.toString())
println(dataClass1.component1())
println(dataClass1.component2())
println(dataClass1.component3())
println(dataClass1.equals(dataClass))
var dataClass2 = dataClass.copy(age = 19)
println(dataClass2.component1())
println(dataClass2.component2())
println(dataClass2.component3())
println(dataClass1.equals(dataClass2))
var ( age,name, sex) = dataClass2
println("name = $name,age=$age,Sex=$sex")
}
运行结果
140.0
DataClass(name=Tom, age=18, sex=男)
150.0
DataClass(name=Tom, age=18, sex=男)
Tom
18
男
true
Tom
19
男
false
name = 19,age=Tom,Sex=男