Scala: Can't get outer class members from inner class reference

自古美人都是妖i 提交于 2019-12-06 07:01:37

I don't know why you expect this to compile. After all, Inner does not have those members, only its enclosing class has them. You can achieve what you want this way:

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!