The difference between kotlin data class and ordinary class

Use restrictions
Let me talk about the restrictions on the use of data class

Data class must have a constructor with parameters
Data class must have at least one primary constructor parameter

2. data class cannot be inherited

Modifier ‘data’ is incompatible with ‘open’


Realize the difference
ordinary class

class VisibilityChangeRecord(val position: Int)
1
into java

public final class VisibilityChangeRecord {
   private final int position;

   public final int getPosition() {
      return this.position;
   }

   public VisibilityChangeRecord(int position) {
      this.position = position;
   }
}



data class

data class VisibilityChangeRecord(val position: Int)


convert to java

public final class VisibilityChangeRecord {
   private final int position;

   public final int getPosition() {
      return this.position;
   }

   public VisibilityChangeRecord(int position) {
      this.position = position;
   }

   public final int component1() {
      return this.position;
   }

   @NotNull
   public final VisibilityChangeRecord copy(int position) {
      return new VisibilityChangeRecord(position);
   }

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

      return var0.copy(var1);
   }

   @NotNull
   public String toString() {
      return "VisibilityChangeRecord(position=" + this.position + ")";
   }

   public int hashCode() {
      return Integer.hashCode(this.position);
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof VisibilityChangeRecord) {
            VisibilityChangeRecord var2 = (VisibilityChangeRecord)var1;
            if (this.position == var2.position) {
               return true;
            }
         }

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


Comparing the two, the data class implements more toString(), hashCode(), equals(), copy(), and componentN() methods than the class.
hashCode() and equals() are used to compare whether the object content is the same, and are mostly used in containers such as HashMap; toString(
) is used to print the object content;
copy() implements the copy function;
componentN() provides quick access to elements Function.
From the above, the functional class of data class can be realized. Data class is just a class provided by kotlin with common data model functions, which is used to improve development efficiency.

Can a little extended
data class have ordinary variables? The answer is yes, so what is the difference between ordinary variables and element variables? See the example below:

data class VisibilityChangeRecord(val position: Int) {
    var id = 0L
}


convert to java

public final class VisibilityChangeRecord {
   private long id;
   private final int position;

   public final long getId() {
      return this.id;
   }

   public final void setId(long var1) {
      this.id = var1;
   }

   public final int getPosition() {
      return this.position;
   }

   public VisibilityChangeRecord(int position) {
      this.position = position;
   }

   public final int component1() {
      return this.position;
   }

   @NotNull
   public final VisibilityChangeRecord copy(int position) {
      return new VisibilityChangeRecord(position);
   }

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

      return var0.copy(var1);
   }

   @NotNull
   public String toString() {
      return "VisibilityChangeRecord(position=" + this.position + ")";
   }

   public int hashCode() {
      return Integer.hashCode(this.position);
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof VisibilityChangeRecord) {
            VisibilityChangeRecord var2 = (VisibilityChangeRecord)var1;
            if (this.position == var2.position) {
               return true;
            }
         }

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


It is found that the ordinary variables in the data class have the same meaning as the ordinary variables of the ordinary class: they do not participate in equals, hashCode, copy, toString, and componentN.
 

Guess you like

Origin blog.csdn.net/liujun3512159/article/details/128843445