【对比Java学Kotlin】数据类

2021/10/2 20:44:39

本文主要是介绍【对比Java学Kotlin】数据类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

我们在 Java 里面会创建一些专门用于盛放数据的类,比如各种以 Bean、Model 作为后缀结尾的类。

这些类的成员变量通常是各种类型的数据,成员函数是 setter 和 getter。或者偷懒的同学直接把成员变量的可见性设置为 public,连 setter 和 getter 都省了。

虽然我们可以省掉 setter&getter 这些模板代码,toString()、equals() 、hashCode() 和 copy() 这些方法还是需要手动实现的。

Kotlin 专门设置了数据类来简化这些模板代码,让代码更加简洁。

我们以 Puppy 这个类为例:

data class Puppy(
    val name: String,
    val age: Int,
    val cuteness: Int,
) {
    var breed: String? = null
}

// 使用方法
val tofu = Puppy(name = "Tofu", age = 1, cuteness = Int.MAX_VALUE)
val taco = Puppy(name = "Taco", age = 2)

fun play() {
    val anotherTaco = taco.copy(name = "Tofu2") // 其余成员变量使用既有值
}

我们来看反编译后的 Java 代码:

public final class Puppy {
   @Nullable
   private String breed;
   @NotNull
   private final String name;
   private final int age;
   @NotNull
   private final String cuteness;

   @Nullable
   public final String getBreed() {
      return this.breed;
   }

   public final void setBreed(@Nullable String var1) {
      this.breed = var1;
   }

   @NotNull
   public final String getName() {
      return this.name;
   }

   public final int getAge() {
      return this.age;
   }

   @NotNull
   public final String getCuteness() {
      return this.cuteness;
   }

   public Puppy(@NotNull String name, int age, @NotNull String cuteness) {
      Intrinsics.checkParameterIsNotNull(name, "name");
      Intrinsics.checkParameterIsNotNull(cuteness, "cuteness");
      super();
      this.name = name;
      this.age = age;
      this.cuteness = cuteness;
   }

   @NotNull
   public final String component1() {
      return this.name;
   }

   public final int component2() {
      return this.age;
   }

   @NotNull
   public final String component3() {
      return this.cuteness;
   }

   @NotNull
   public final Puppy copy(@NotNull String name, int age, @NotNull String cuteness) {
      Intrinsics.checkParameterIsNotNull(name, "name");
      Intrinsics.checkParameterIsNotNull(cuteness, "cuteness");
      return new Puppy(name, age, cuteness);
   }

   // $FF: synthetic method
   public static Puppy copy$default(Puppy 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.cuteness;
      }

      return var0.copy(var1, var2, var3);
   }

   @NotNull
   public String toString() {
      return "Puppy(name=" + this.name + ", age=" + this.age + ", cuteness=" + this.cuteness + ")";
   }

   public int hashCode() {
      String var10000 = this.name;
      int var1 = ((var10000 != null ? var10000.hashCode() : 0) * 31 + this.age) * 31;
      String var10001 = this.cuteness;
      return var1 + (var10001 != null ? var10001.hashCode() : 0);
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof Puppy) {
            Puppy var2 = (Puppy)var1;
            if (Intrinsics.areEqual(this.name, var2.name) && this.age == var2.age && Intrinsics.areEqual(this.cuteness, var2.cuteness)) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }
}

可以看到,data class 为每个属性:

  • 自动生成了 getter&setter
  • 自动使用主构造函数中的属性生成 equals() & hashCode() & copy() & toString()方法

简直不要太方便!终于可以摆脱烦人的模板代码了!

在反编译的 Java 代码中有几个函数比较显眼:

   @NotNull
   public final String component1() {
      return this.name;
   }

   public final int component2() {
      return this.age;
   }

   @NotNull
   public final String component3() {
      return this.cuteness;
   }

什么是 component?作用是啥?在什么场景下用?

同时,我们需要注意:

  • 数据类不能是 abstract, open, sealed, 或者 inner 的,否则会报错
  • 数据类可以继承其他的类或者接口或者抽象类,但是不能继承其他的数据类
  • 数据类的主构造方法中只能是val/var类型的成员变量,只有主构造方法中的成员变量才会在被用于生成equals() & hashCode() & copy() & toString()方法,类体内的成员变量则不会
  • 数据类主构造方法中的成员变量建议使用 val 类型,因为当数据类的实例作为 HashMap 等容器的 key 使用时,var 类型的变量的改变会导致我们得到错误的 value

Koltin 内置了 Pair 和 Triple 这两个数据类,分别用于承载两个和三个成员变量的场景。



这篇关于【对比Java学Kotlin】数据类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程