Kotlin学习第1篇——data class

Data classes 官方介绍

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

文章目录

  • [1 输出结果是什么?](#1 输出结果是什么?)
  • [2 输出结果是什么?](#2 输出结果是什么?)
  • [3 输出的结果是什么?](#3 输出的结果是什么?)
  • [4 下面哪些data class 可以编译通过?](#4 下面哪些data class 可以编译通过?)

1 输出结果是什么?

kotlin 复制代码
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 输出结果是什么?

kotlin 复制代码
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 输出的结果是什么?

kotlin 复制代码
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 可以编译通过?

kotlin 复制代码
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

kotlin 复制代码
data class DataBean(var title: String = "", val subTitle: String = "")

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

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;
      }
   }
}
相关推荐
wuhen_n3 分钟前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
不会代码的小测试6 分钟前
UI自动化-POM封装
开发语言·python·selenium·自动化
roman_日积跬步-终至千里12 分钟前
【Java并发】Java 线程池实战:警惕使用CompletableFuture.supplyAsync
java·开发语言·网络
lsx20240617 分钟前
C++ 基本的输入输出
开发语言
ZH154558913118 分钟前
Flutter for OpenHarmony Python学习助手实战:GUI桌面应用开发的实现
python·学习·flutter
儿歌八万首18 分钟前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
CodeSheep程序羊29 分钟前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
独好紫罗兰30 分钟前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
I'mChloe38 分钟前
PTO-ISA 深度解析:PyPTO 范式生成的底层指令集与 NPU 算子执行的硬件映射
c语言·开发语言
编程小白20261 小时前
从 C++ 基础到效率翻倍:Qt 开发环境搭建与Windows 神级快捷键指南
开发语言·c++·windows·qt·学习