Ric*_*ver 2 scala inner-classes
我似乎无法从内部类引用中获取外部类成员:
class Outer(st: Int)
{
val valOut = st
def f = 4
class Inner { val x = 5 }
}
object myObj {
val myOut = new Outer(8)
val myIn = new myOut.Inner
val myVal: Int = myIn.valOut//value f is not a member of ... myOut.Inner
val x = myIn.f//value valOut is not a member of ... myOut.Inner
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这个内部包,并且在eclipse工作表中都不起作用.我在eclipse 3.7.2中使用Scala 2.10.0RC1和Scala插件2.1.0M2
Kim*_*bel 13
我不知道为什么你期望这个编译.毕竟,Inner没有那些成员,只有它的封闭类有它们.你可以通过这种方式实现你想要的:
class Outer(st: Int) {
val valOut = st
def f = 4
class Inner {
val outer = Outer.this
val x = 5
}
}
object myObj {
val myOut = new Outer(8)
val myIn = new myOut.Inner
val myVal: Int = myIn.outer.valOut
val x = myIn.outer.f
}
Run Code Online (Sandbox Code Playgroud)