SPDX-License-Identifier: Apache-2.0 */
data class Puppy(
val name: String,
val breed: String,
var cuteness: Int = 11
// 创建新的实例
val tofuPuppy = Puppy(name = "Tofu", breed = "Corgi", cuteness = Int.MAX_VALUE)
val tacoPuppy = Puppy(name = "Taco", breed = "Cockapoo")
// 访问和修改属性
val breed = tofuPuppy.breed
tofuPuppy.cuteness++
// 解构
val (name, breed, cuteness) = tofuPuppy
println(name) // prints: "Tofu"
// 拷贝:使用与 tofuPuppy 相同的品种和可爱度创建一个小狗,但名字不同
SPDX-License-Identifier: Apache-2.0 */
data class Puppy constructor(
val name: String,
val breed: String,
var cuteness: Int = 11,
// 错误:数据类的的主构造函数中只能包含属性 (val 或 var) 参数
playful: Boolean,
// 错误:数据类型的主构造函数已禁用 vararg 参数
vararg friends: Puppy
SPDX-License-Identifier: Apache-2.0 */
public final class Puppy {
@NotNull
private final String name;
@NotNull
private final String breed;
private int cuteness;
@NotNull
public final String getName() {
return this.name;
@NotNull
public final String getBreed() {
return this.breed;
public final int getCuteness() {
return this.cuteness;
public final void setCuteness(int var1) {
this.cuteness = var1;
SPDX-License-Identifier: Apache-2.0 */
public Puppy(@NotNull String name, @NotNull String breed, int cuteness) {
this.name = name;
this.breed = breed;
this.cuteness = cuteness;
// $FF: synthetic method
public Puppy(String var1, String var2, int var3, int var4, DefaultConstructorMarker var5) {
if ((var4 & 4) != 0) {
var3 = 11;
this(var1, var2, var3);
SPDX-License-Identifier: Apache-2.0 */
@NotNull
public String toString() {
return "Puppy(name=" + this.name + ", breed=" + this.breed + ", cuteness=" + this.cuteness + ")";
public int hashCode() {
String var10000 = this.name;
int var1 = (var10000 != null ? var10000.hashCode() : 0) * 31;
String var10001 = this.breed;
return (var1 + (var10001 != null ? var10001.hashCode() : 0)) * 31 + this.cuteness;
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof Puppy) {
Puppy var2 = (Puppy)var1;
if (Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.breed, var2.breed) && this.cuteness == var2.cuteness) {
return true;
return false;
} else {
return true;
...
toString
和
hashCode
函数的实现很直接,跟一般您所实现的类似,而 equals 使用了
Intrinsics.areEqual
以实现结构化比较:
public static boolean areEqual(Object first, Object second) {
return first == null ? second == null : first.equals(second);
SPDX-License-Identifier: Apache-2.0 */
@NotNull
public final String component1() {
return this.name;
@NotNull
public final String component2() {
return this.breed;
public final int component3() {
return this.cuteness;
SPDX-License-Identifier: Apache-2.0 */
@NotNull
public final Puppy copy(@NotNull String name, @NotNull String breed, int cuteness) {
Intrinsics.checkNotNullParameter(name, "name");
Intrinsics.checkNotNullParameter(breed, "breed");
return new Puppy(name, breed, cuteness);
// $FF: synthetic method
public static Puppy copy$default(Puppy var0, String var1, String var2, int var3, int var4, Object var5) {
if ((var4 & 1) != 0) {
var1 = var0.name;
if ((var4 & 2) != 0) {
var2 = var0.breed;
if ((var4 & 4) != 0) {