2

我似乎无法从内部类引用中获取外部类成员:

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
}

我已经在包内尝试过这个,并且在 eclipse 工作表中都不起作用。我在 Eclipse 3.7.2 中使用 Scala 2.10.0RC1 和 Scala 插件 2.1.0M2

4

1 回答 1

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
}
于 2012-10-24T10:15:47.040 回答