Scala:无法从内部类引用中获取外部类成员

时间:2012-10-24 10:10:32

标签: 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
}

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

1 个答案:

答案 0 :(得分:12)

我不知道为什么你希望这个编译。毕竟,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
}