57.数据类

作者: 写代码的向日葵 | 来源:发表于2019-10-05 01:37 被阅读0次

1.写法如下

fun main(args: Array<String>) {

}

data class News(var title: String, var desc: String, var imgPath: String, var content: String)

2.看下转换后的java代码

public final class News {
   @NotNull
   private String title;
   @NotNull
   private String desc;
   @NotNull
   private String imgPath;
   @NotNull
   private String content;

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

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

   @NotNull
   public final String getDesc() {
      return this.desc;
   }

   public final void setDesc(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.desc = var1;
   }

   @NotNull
   public final String getImgPath() {
      return this.imgPath;
   }

   public final void setImgPath(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.imgPath = var1;
   }

   @NotNull
   public final String getContent() {
      return this.content;
   }

   public final void setContent(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.content = var1;
   }

   public News(@NotNull String title, @NotNull String desc, @NotNull String imgPath, @NotNull String content) {
      Intrinsics.checkParameterIsNotNull(title, "title");
      Intrinsics.checkParameterIsNotNull(desc, "desc");
      Intrinsics.checkParameterIsNotNull(imgPath, "imgPath");
      Intrinsics.checkParameterIsNotNull(content, "content");
      super();
      this.title = title;
      this.desc = desc;
      this.imgPath = imgPath;
      this.content = content;
   }

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

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

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

   @NotNull
   public final String component4() {
      return this.content;
   }

   @NotNull
   public final News copy(@NotNull String title, @NotNull String desc, @NotNull String imgPath, @NotNull String content) {
      Intrinsics.checkParameterIsNotNull(title, "title");
      Intrinsics.checkParameterIsNotNull(desc, "desc");
      Intrinsics.checkParameterIsNotNull(imgPath, "imgPath");
      Intrinsics.checkParameterIsNotNull(content, "content");
      return new News(title, desc, imgPath, content);
   }

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

      if ((var5 & 2) != 0) {
         var2 = var0.desc;
      }

      if ((var5 & 4) != 0) {
         var3 = var0.imgPath;
      }

      if ((var5 & 8) != 0) {
         var4 = var0.content;
      }

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

   @NotNull
   public String toString() {
      return "News(title=" + this.title + ", desc=" + this.desc + ", imgPath=" + this.imgPath + ", content=" + this.content + ")";
   }

   public int hashCode() {
      String var10000 = this.title;
      int var1 = (var10000 != null ? var10000.hashCode() : 0) * 31;
      String var10001 = this.desc;
      var1 = (var1 + (var10001 != null ? var10001.hashCode() : 0)) * 31;
      var10001 = this.imgPath;
      var1 = (var1 + (var10001 != null ? var10001.hashCode() : 0)) * 31;
      var10001 = this.content;
      return var1 + (var10001 != null ? var10001.hashCode() : 0);
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof News) {
            News var2 = (News)var1;
            if (Intrinsics.areEqual(this.title, var2.title) && Intrinsics.areEqual(this.desc, var2.desc) && Intrinsics.areEqual(this.imgPath, var2.imgPath) && Intrinsics.areEqual(this.content, var2.content)) {
               return true;
            }
         }

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

3.数据类的访问

fun main(args: Array<String>) {
    val news = News("标题", "描述", "图片", "内部")
    println(news.title)
    println(news.component1())//第一个元素
    println(news.component2())//第二个元素
    //数据类的结构
    val (title, desc, imgPath, content) = News("标题", "描述", "图片", "内部")
    println(title)
    println(desc)
    println(imgPath)
    println(content)
}

data class News(var title: String, var desc: String, var imgPath: String, var content: String)

相关文章

  • 57.数据类

    1.写法如下 2.看下转换后的java代码 3.数据类的访问

  • 练习SQL利器,牛客网SQL实战题库,57~61题

    57.使用含有关键字exists查找未分配具体部门的员工的所有信息 58.获取employees中的行数据,且这些...

  • Kotlin基本语法之(六) 数据类data与单例类object

    数据类data data类是Kotlin中专门用来描述数据的类,数据类通常指的就是实体类(bean/entity)...

  • 57.

    人的需求并没有随着时间改变,不管是青年、中年、还是老年。你们人发育成熟的那一刻,大部分需求就已经定型了。尤其是心理...

  • 57.

    人如果没有梦想,那和咸鱼有什么区别? 顾十说咸鱼翻身还是咸鱼,杨二说要是咸鱼连翻身都不想翻身的话,那这辈子怕是也没...

  • LintCode 57. 3Sum

    原题 LintCode 57. 3Sum Description Given an array S of n in...

  • 区间合并算法

    0X00 区间合并 803. 区间合并 57. 插入区间

  • Leetcode 57 | 插入区间

    题目 Leetcode地址:57. 插入区间[https://leetcode.cn/problems/inser...

  • 数据类

    kotlin定义了一种 专门用于保存数据 的类——数据类,关键字为data class, 它有以下2个要求: 默认...

  • 数据类

    我们经常创建一些只保存数据的类。 在这些类中,一些标准函数往往是从数据机械推导而来的。在 Kotlin 中,这叫做...

网友评论

    本文标题:57.数据类

    本文链接:https://www.haomeiwen.com/subject/wdlepctx.html