Kotlin中数据bean的实现原理

2022-10-08  本文已影响0人  ModestStorm

1.声明数据bean类

data class PersonBean(var name:String)

2.反编译查看对应的java文件

public final class PersonBean {
   @NotNull
   private String name;//属性声明

  // 1.生成了属性对应的get方法
   @NotNull
   public final String getName() {
      return this.name;
   }

   //2.生成了属性对应的get方法
   public final void setName(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.name = var1;
   }

  // 3.生成了构造方法
   public PersonBean(@NotNull String name) {
      Intrinsics.checkParameterIsNotNull(name, "name");
      super();
      this.name = name;
   }

   @NotNull
   public final String component1() {
      return this.name;
   }
   // 3.复制实例对象
   @NotNull
   public final PersonBean copy(@NotNull String name) {
      Intrinsics.checkParameterIsNotNull(name, "name");
      return new PersonBean(name);
   }

   
   // 4.根据属性重写了toString
   @NotNull
   public String toString() {
      return "PersonBean(name=" + this.name + ")";
   }
  //5.根据属性重写了hashCode
   public int hashCode() {
      String var10000 = this.name;
      return var10000 != null ? var10000.hashCode() : 0;
   }

  //6.根据属性重写了equals
   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof PersonBean) {
            PersonBean var2 = (PersonBean)var1;
            if (Intrinsics.areEqual(this.name, var2.name)) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }
}
上一篇下一篇

猜你喜欢

热点阅读