Scala:无法从内部类参考中获取外部类成员 [英] Scala: Can't get outer class members from inner class reference

查看:58
本文介绍了Scala:无法从内部类参考中获取外部类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't seem to get the outer class members from an inner class reference:

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工作表中都没有用.我在带有Scala插件2.1.0M2的Eclipse 3.7.2中使用Scala 2.10.0RC1

I've tried this inside packages and in the eclipse worksheet neither works. I'm using Scala 2.10.0RC1 in eclipse 3.7.2 with Scala plug-in 2.1.0M2

推荐答案

我不知道您为什么希望编译它.毕竟,Inner没有这些成员,只有其封闭的类具有这些成员.您可以通过这种方式实现您想要的:

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
}

这篇关于Scala:无法从内部类参考中获取外部类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆