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.数据类

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