Kotlin singletons and companion objects

1. Kotlin singleton implementation method:
modifier object

饿汉式,懒汉式,DCL模式与java一致

2. Companion objects

1. Modifier companion

 companion object comA {
    }

decompile:

 public static final class comA {
      private comA() {
      }

      // $FF: synthetic method
      public comA(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }

It is the same as the static method, you can directly call the class name. Method

注意:

每个类可以对应一个伴生对象
伴生对象的成员全局只有一个!!!!!
伴生对象的成员类似于java的静态成员

Guess you like

Origin blog.csdn.net/github_37610197/article/details/128975062