Kotlin单例、数据类、静态

文章目录

  • [1. 数据类](#1. 数据类)
  • [2. 单例](#2. 单例)
  • [3. 静态](#3. 静态)

1. 数据类

kotlin 复制代码
data class Cellphone(val brand: String, val price: Double)

自动生成了get set hashcode equals toString等方法。通过反编译(打开AS,在Tools-Kotlin-ShowBytecode-Decompile)可以得到如下结果

kotlin 复制代码
public final class Cellphone {
   @NotNull
   private final String brand;
   private final double price;

   @NotNull
   public final String getBrand() {
      return this.brand;
   }

   public final double getPrice() {
      return this.price;
   }

   public Cellphone(@NotNull String brand, double price) {
      Intrinsics.checkNotNullParameter(brand, "brand");
      super();
      this.brand = brand;
      this.price = price;
   }

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

   public final double component2() {
      return this.price;
   }

   @NotNull
   public final Cellphone copy(@NotNull String brand, double price) {
      Intrinsics.checkNotNullParameter(brand, "brand");
      return new Cellphone(brand, price);
   }

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

      if ((var4 & 2) != 0) {
         var2 = var0.price;
      }

      return var0.copy(var1, var2);
   }

   @NotNull
   public String toString() {
      return "Cellphone(brand=" + this.brand + ", price=" + this.price + ")";
   }

   public int hashCode() {
      String var10000 = this.brand;
      return (var10000 != null ? var10000.hashCode() : 0) * 31 + Double.hashCode(this.price);
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof Cellphone) {
            Cellphone var2 = (Cellphone)var1;
            if (Intrinsics.areEqual(this.brand, var2.brand) && Double.compare(this.price, var2.price) == 0) {
               return true;
            }
         }

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

2. 单例

class改成object,其他的跟java里面差不多

kotlin 复制代码
object Singleton {
}

3. 静态

companion object

@JvmField - 修饰静态变量

@JvmStatic - 修饰静态方法

@JvmField 和 @JvmStatic 只能写在 object 修饰的类或者 companion object 里,写法虽然有些别扭,但是效果是真的是按 static 来实现的

kotlin 复制代码
class BookKotlin {
    companion object {
        @JvmField
        var nameStatic: String = "BB"
        @JvmStatic
        fun speakStatic() {
        }
    }
}
相关推荐
西瓜本瓜@1 小时前
在Android中如何使用Protobuf上传协议
android·java·开发语言·git·学习·android-studio
UFIT1 小时前
Python函数与模块笔记
开发语言·python
言之。1 小时前
别学了,打会王者吧
java·python·mysql·容器·spark·php·html5
机智的人猿泰山1 小时前
java kafka
java·开发语言·kafka
Y1nhl2 小时前
搜广推校招面经八十一
开发语言·人工智能·pytorch·深度学习·机器学习·推荐算法·搜索算法
Algorithm15762 小时前
谈谈接口和抽象类有什么区别?
java·开发语言
yu4106212 小时前
Rust 语言使用场景分析
开发语言·后端·rust
良艺呐^O^2 小时前
uniapp实现app自动更新
开发语言·javascript·uni-app
细心的莽夫2 小时前
SpringCloud 微服务复习笔记
java·spring boot·笔记·后端·spring·spring cloud·微服务
264玫瑰资源库4 小时前
问道数码兽 怀旧剧情回合手游源码搭建教程(反查重优化版)
java·开发语言·前端·游戏