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

前端 未结 2 1888
我在风中等你
我在风中等你 2020-12-14 14:22

I want to access a member of the MainFragment class from PersonAdapter class but none of them are available. I tried making both the classes and the members public and priva

2条回答
  •  被撕碎了的回忆
    2020-12-14 15:08

    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{
    
           //....
        }
    
    }
    

提交回复
热议问题