
上QQ阅读APP看书,第一时间看更新
2.2.4 嵌套类和内部类
1.嵌套类(Nested Class)
Kotlin的嵌套类是指定义在某一个类内部的类,嵌套类不能够访问外部类的成员,除非嵌套类变成内部类。

如果嵌套类想访问外部类的属性,就会报错,如图2-5所示。
2.内部类(Inner Class)
Kotlin的内部类使用inner关键字标识,内部类能够访问外部类的成员。


图2-5 嵌套类想访问外部类的属性
小结一下嵌套类和内部类:
· 默认的是嵌套类。
· 嵌套类不持有外部类的引用,内部类持有外部类的引用。
· 嵌套类的创建方式:外部类.嵌套类()。
· 内部类的创建方式:外部类().内部类()。