Kotlin学习第1篇——data class

发布时间:2024年01月16日

Data classes 官方介绍

在学习之前带着问题往往能够收获颇丰

1 输出结果是什么?

data class Student(var name: String = ""){
   var age:Int = 0
}

val a = Student(name = "1")
val b = Student(name = "1")
Log.d("GerryLiang", "a equals b ?${a.equals(b)}")
Log.d("GerryLiang", "a == b ?${a == b}")
Log.d("GerryLiang", "a === b ?${a === b}")

2 输出结果是什么?

data class Student(var name: String = ""){
   var age:Int = 0
}

val a = Student(name = "1").apply {
    age = 10
}
val b = Student(name = "1").apply {
    age = 20
}
Log.d("GerryLiang", "a equals b ?${a.equals(b)}")
Log.d("GerryLiang", "a == b ?${a == b}")
Log.d("GerryLiang", "a === b ?${a === b}")

3 输出的结果是什么?

data class Student(var name: String) {
    override fun toString(): String {
        return "Gerry toString"
    }
}

 val a = Student(name = "1")
 Log.d("GerryLiang", "$a")

4 下面哪些data class 可以编译通过?

data class Student()
abstract data class Student2(var name: String = "")
open data class Student3(var name: String = "", var age: Int = 0)
inner data class Student4(var name: String = "", var age: Int = 0)
sealed data class Student5(var name: String = "", var age: Int = 0)

下面开始公布答案:

  1. true true false (没有笔误,可以自测下)
  2. true true false (没有笔误,可以自测下)
  3. Gerry toString
  4. 都不可以

如何学习? 查看反编译后的Java代码即可!

如下所示一个简单的 data class

data class DataBean(var title: String = "", val subTitle: String = "")

我们来看看反编译后的Java代码长什么样子

public final class DataBean {
   // 每个属性 内部生成了一系列的 get 和 set 方法
   @NotNull
   private String title;
   @NotNull
   private final String subTitle;

   @NotNull
   public final String getTitle() {
      return this.title;
   }

   public final void setTitle(@NotNull String var1) {
      Intrinsics.checkNotNullParameter(var1, "<set-?>");
      this.title = var1;
   }

   @NotNull
   public final String getSubTitle() {
      return this.subTitle;
   }

   public DataBean(@NotNull String title, @NotNull String subTitle) {
      Intrinsics.checkNotNullParameter(title, "title");
      Intrinsics.checkNotNullParameter(subTitle, "subTitle");
      super();
      this.title = title;
      this.subTitle = subTitle;
   }

   // $FF: synthetic method
   public DataBean(String var1, String var2, int var3, DefaultConstructorMarker var4) {
      if ((var3 & 1) != 0) {
         var1 = "";
      }

      if ((var3 & 2) != 0) {
         var2 = "";
      }

      this(var1, var2);
   }

   public DataBean() {
      this((String)null, (String)null, 3, (DefaultConstructorMarker)null);
   }

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

   @NotNull
   public final String component2() {
      return this.subTitle;
   }

   @NotNull
   public final DataBean copy(@NotNull String title, @NotNull String subTitle) {
      Intrinsics.checkNotNullParameter(title, "title");
      Intrinsics.checkNotNullParameter(subTitle, "subTitle");
      return new DataBean(title, subTitle);
   }

   // $FF: synthetic method
   public static DataBean copy$default(DataBean var0, String var1, String var2, int var3, Object var4) {
      if ((var3 & 1) != 0) {
         var1 = var0.title;
      }

      if ((var3 & 2) != 0) {
         var2 = var0.subTitle;
      }

      return var0.copy(var1, var2);
   }

// 以后不用自己拼接字符串打印参数了
   @NotNull
   public String toString() {
      return "DataBean(title=" + this.title + ", subTitle=" + this.subTitle + ")";
   }

// 重写了hashCode 
   public int hashCode() {
      String var10000 = this.title;
      int var1 = (var10000 != null ? var10000.hashCode() : 0) * 31;
      String var10001 = this.subTitle;
      return var1 + (var10001 != null ? var10001.hashCode() : 0);
   }
   
// 重写了equals方法
   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof DataBean) {
            DataBean var2 = (DataBean)var1;
            if (Intrinsics.areEqual(this.title, var2.title) && Intrinsics.areEqual(this.subTitle, var2.subTitle)) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }
}
文章来源:https://blog.csdn.net/LosingCarryJie/article/details/135612935
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。