I can't reach any class member from a nested class in Kotlin

心不动则不痛 提交于 2019-11-27 22:44:02

In Kotlin, nested classes cannot access the outer class instance by default, just like nested static classes in Java.

To do that, add the inner modifier to the nested class:

class MainFragment : Fragment() {
    // ...

    inner class PersonAdapter() : RecyclerView.Adapter<ViewHolder>() {
        // ...
    }
}

See: Nested classes in the language reference

In Kotlin, there are 2 types of the nested classes.

  1. Nested Class
  2. inner Class

Nested class are not allowed to access the member of the outer class.

If you want to access the member of outer class in the nested class then you need to define that nested class as inner class.

class OuterClass{

    var name="john"

    inner class InnerClass{

       //....
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!