Static Inner Class in Kotlin

前端 未结 2 1023
忘了有多久
忘了有多久 2020-12-29 20:12

What alternative to an Inner static Class can I use in Kotlin Language, if it exists? If not, how can I solve this problem when I need to use a static cla

相关标签:
2条回答
  • 2020-12-29 20:29

    You can also change the "class" to "object"

    class OuterA {
      object InnerB {
      ... }
    }
    

    OR

    object OuterA {
      object InnerB {
      ... }
    }
    
    0 讨论(0)
  • 2020-12-29 20:35

    Just omit the inner in Kotlin.

    Inner class (holding reference to outer object)

    Java:

    class A {
        class B {
        ...
        }
    }
    

    Kotlin:

    class A {
        inner class B {
        ...
        }
    }
    

    Static inner class aka nested class (no reference to outer object)

    Java:

    class A {
        static class B {
        ...
        }
    }
    

    Kotlin:

    class A {
        class B {
        ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题