Kotlin - 44. Java 호출 kotlin 의 2 (Call Kotlin from Java)

공식 문서:http://kotlinlang.org/docs/reference/java-to-kotlin-interop.html
4. 정적 필드 (정적 필드)
   (object)     (companion object)    Kotlin  ,        (backing fields)!
         private,          (  public):
    @JvmField   
    lateinit    
    const    

1. @JvmField    ,    java     kotlin       (  public)
    // kotlin
    class Key(val value: Int) {
        companion object {
            @JvmField
            val COMPARATOR: Comparator = compareBy { it.value }
        }
    }

    // Java
    Key.COMPARATOR; // COMPARATOR    public static final

2. lateinit    ,              setter     (  public)
    // kotlin
    object Singleton {            
        lateinit var provider: Provider // lateinit      
    }

    // Java
    Singleton.provider = new Provider(); // provider    public static  final

3. const   (        )  , Java    public    
    // kotlin  (example.kt  )
    object Obj {
        const val CONST = 1
    }
    class C {
        companion object {
            const val VERSION = 9
        }
    }
    const val MAX = 239

    // Java  
    int c = Obj.CONST;
    int v = C.VERSION;
    int d = ExampleKt.MAX;

5. 정적 방법 (정적 방법)
     (object)     (companion object)     @JvmStatic  ,
                    ,              !
  ,@JvmStatic                ,  getter setter       !
  1:
    // kotlin
    class C {
        companion object {
            @JvmStatic fun foo() {}
            fun bar() {}
        }
    }

    // Java
    C.foo(); // OK,    
    C.bar(); //   ,      
    C.Companion.foo(); // OK,    
    C.Companion.bar(); // OK,    

  2:
    // kotlin     
    object Obj {
        @JvmStatic fun foo() {}
        fun bar() {}
    }

    // Java
    Obj.foo(); // OK,    
    Obj.bar(); //   ,      
    Obj.INSTANCE.bar(); // OK,    
    Obj.INSTANCE.foo(); // OK,    

6. 가시 성 (가시 성)
Kotlin    Java     :
    private     Java private  ;
    private       Java       (package-local);
    protected   Java protected(Java           protected  ,  Java        );
    internal     Java public;
    public   Java public;

7.KClass
   java   Kotlin  ( KClass    ),    Java Class     kotlin KClass,
        Class.kotlin         :
    kotlin.jvm.JvmClassMappingKt.getKotlinClass(MainView.class)

:http://www.jianshu.com/p/042ea2c9062c CSDN 블 로그:http://blog.csdn.net/qq_32115439 / article / details / 75332128 GitHub 블 로그:http://lioil.win/2017/07/18/Kotlin-kotlinInJava2.html 코딩 블 로그:http://c.lioil.win/2017/07/18/Kotlin-kotlinInJava2.html

좋은 웹페이지 즐겨찾기