Kotlin Class-内部类

时间:2024-07-14 07:10:37
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);
      }
   }
   )
}