Kotlin单例、数据类、静态

1. 数据类

data class Cellphone(val brand: String, val price: Double)

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

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里面差不多

object Singleton {
}

3. 静态

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

class BookKotlin {
    companion object {
        @JvmField
        var nameStatic: String = "BB"
        @JvmStatic
        fun speakStatic() {
        }
    }
}

相关推荐

  1. Kotlin数据静态

    2024-07-21 23:28:02       18 阅读
  2. kotlin

    2024-07-21 23:28:02       53 阅读
  3. 设计模式-模式(静态内部

    2024-07-21 23:28:02       30 阅读
  4. 模式之静态内部与枚举

    2024-07-21 23:28:02       21 阅读
  5. Kotlin 数据

    2024-07-21 23:28:02       48 阅读
  6. Kotlin数据

    2024-07-21 23:28:02       57 阅读
  7. 【c/c++】C++静态工具模式对比学习

    2024-07-21 23:28:02       48 阅读
  8. Kotlin模式(项目使用实例)

    2024-07-21 23:28:02       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 23:28:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 23:28:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 23:28:02       45 阅读
  4. Python语言-面向对象

    2024-07-21 23:28:02       55 阅读

热门阅读

  1. CSP-J模拟赛day1

    2024-07-21 23:28:02       19 阅读
  2. Linux下双网卡NAT组网

    2024-07-21 23:28:02       18 阅读
  3. Node的API基础

    2024-07-21 23:28:02       17 阅读
  4. C2W3.LAB.N-grams+Language Model+OOV

    2024-07-21 23:28:02       17 阅读
  5. 力扣题解(一和零)

    2024-07-21 23:28:02       20 阅读
  6. urllib&requests

    2024-07-21 23:28:02       15 阅读
  7. 接到需求后的开发步骤

    2024-07-21 23:28:02       17 阅读
  8. C#WPF九宫格图片背景实例

    2024-07-21 23:28:02       17 阅读
  9. 算法学习4——动态规划

    2024-07-21 23:28:02       19 阅读