Kotlin Class

admin2024-07-10  8

普通 Class

kotlin

class TestClz {
    val name: String = "hsw"
    val age: Int = 18
}

java

public final class TestClz {
   @NotNull
   private final String name = "hsw";
   private final int age = 18;

   @NotNull
   public final String getName() {
      return this.name;
   }

   public final int getAge() {
      return this.age;
   }
}

转换为Java后的变化

  1. 类被 final 修饰,不可继承;加上open 可以去掉 final
  2. var修饰的属性添加了get set 方法 , val 只有·get 方法

data class

kotlin

data class TestClz(val name: String, var age: Int)

java

public final class TestClz {
   @NotNull
   private final String name;
   private int age;

   @NotNull
   public final String getName() {
      return this.name;
   }

   public final int getAge() {
      return this.age;
   }

   public final void setAge(int var1) {
      this.age = var1;
   }

   public TestClz(@NotNull String name, int age) {
      Intrinsics.checkNotNullParameter(name, "name");
      super();
      this.name = name;
      this.age = age;
   }

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

   public final int component2() {
      return this.age;
   }

   @NotNull
   public final TestClz copy(@NotNull String name, int age) {
      Intrinsics.checkNotNullParameter(name, "name");
      return new TestClz(name, age);
   }

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

      if ((var3 & 2) != 0) {
         var2 = var0.age;
      }

      return var0.copy(var1, var2);
   }

   @NotNull
   public String toString() {
      return "TestClz(name=" + this.name + ", age=" + this.age + ")";
   }

   public int hashCode() {
      String var10000 = this.name;
      return (var10000 != null ? var10000.hashCode() : 0) * 31 + Integer.hashCode(this.age);
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof TestClz) {
            TestClz var2 = (TestClz)var1;
            if (Intrinsics.areEqual(this.name, var2.name) && this.age == var2.age) {
               return true;
            }
         }

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

转换为Java后的变化

  1. 类被 final 修饰,不可继承;加上open 可以去掉 final
  2. var修饰的属性添加了get set 方法 , val 只有·get 方法
  3. 实现了 toString equal hashCode 方法
  4. 类似于java bean

object

object TestClz {
    val name: String = "hsw"
    var age: Int = 18
    fun getInfo(): String {
        return "${name}-${age}"
    }
}
public final class TestClz {
   @NotNull
   private static final String name;
   private static int age;
   @NotNull
   public static final TestClz INSTANCE;

   @NotNull
   public final String getName() {
      return name;
   }

   public final int getAge() {
      return age;
   }

   public final void setAge(int var1) {
      age = var1;
   }

   @NotNull
   public final String getInfo() {
      return name + '-' + age;
   }

   private TestClz() {
   }

   static {
      TestClz var0 = new TestClz();
      INSTANCE = var0;
      name = "hsw";
      age = 18;
   }
}

转换为Java后的变化

  1. 类被 final 修饰,不可继承;加上open 可以去掉 final
  2. var修饰的属性添加了get set 方法 , val 只有·get 方法
  3. 饿汉式实现了单例
  4. java 不可直接调用get set 需要通过INSTANCE , 属性和方法 加上@JvmStatic 可以转换为静态属性/方法 直接调用

内部类

class TestClz {
    val name: String = "hsw"

    companion object {

        val grade = 12L

        fun getInfo() {
            println("name-$name")
        }
    }

    object CCS {
        val grade = 12L

        fun getSome() {
            println("name-$name")
        }
    }

    class Info {
        val grade = 12L

        fun getSome() {
            println("name-$name")
        }
    }

    inner class WC {
        val grade = 12L

        fun getSome() {
            println("name-${name}")
        }
    }
}
public final class TestClz {
   @NotNull
   private final String name = "hsw";
   private static final long grade = 12L;
   @NotNull
   public static final Companion Companion = new Companion((DefaultConstructorMarker)null);

   @NotNull
   public final String getName() {
      return this.name;
   }

	// companion 伴生对象
	// kotlin 可直接访问属性和方法
	// java 需要通过 Companion 对象访问
	 public static final class Companion {
      public final long getGrade() {
         return TestClz.grade;
      }

      public final void getInfo() {
         
      }

      private Companion() {
      }

      // $FF: synthetic method
      public Companion(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }
   // object 相当于通过单例实现静态内部类对象
   public static final class CCS {
      private static final long grade;
      @NotNull
      public static final CCS INSTANCE;

      public final long getGrade() {
         return grade;
      }

      public final void getSome() {
    
      }

      private CCS() {
      }

      static {
         CCS var0 = new CCS();
         INSTANCE = var0;
         grade = 12L;
      }
   }

	// 普通 class 相当于静态内部类,需要实例化调用方法
   public static final class Info {
      private final long grade = 12L;

      public final long getGrade() {
         return this.grade;
      }

      public final void getSome() {
         
      }
   }

	// inner class 内部类,可以直接使用外部类的属性和方法
   public final class WC {
      private final long grade = 12L;

      public final long getGrade() {
         return this.grade;
      }

      public final void getSome() {
         String var1 = "name-" + TestClz.this.getName();
         System.out.println(var1);
      }
   }
   )
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!