作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么我不能在内部类的实例上访问外部类的属性?
class A(val id: String) {
inner class B {}
}
fun test() {
val a = A("test")
val b = a.B()
aid(a)
bid(b)
}
fun aid(a:A): String = a.id
fun bid(b:A.B): String = b.id //Unresolved reference: id
this@A.id
的 setter/getter .但为什么?
最佳答案
内部类仅具有对封闭实例的引用,因此不继承外部类的成员。
由于内部类具有对封闭类的引用,因此只能在类中访问此封闭实例(Java:Outer.this
,Kotlin:this@Outer
),但是您不能从内部外部访问封闭实例是正确的类(class)。
A class may be marked as
inner
to be able to access members of outer class.
Kotlin Reference / Nested and Inner Classes
关于kotlin - 在 Kotlin 中,为什么我不能在内部类的实例上访问外部类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52088622/
我是一名优秀的程序员,十分优秀!