为什么我无法访问 Scala 中类的伴生对象中的私有类方法?

发布于 2024-09-29 09:54:40 字数 854 浏览 5 评论 0原文

我正在为我的面向对象设计课程做家庭作业,但我在使用 Scala 的伴生对象时遇到了麻烦。我在一些地方读到,伴生对象应该能够访问其伴生类的私有方法,但我似乎无法让它工作。 (请注意,作业的主要内容与实现二叉搜索树有关,所以我不仅仅是寻求答案......)

我有一个对象应该创建我的私有类的实例, BstAtlas(Bst也在Atlas对象中定义,为了清楚起见将其取出):

object Atlas {                                             
  def focusRoom(newRoom:Room,a:Atlas):Atlas = a.helpFocusRoom(newRoom);

  abstract class Atlas {
    ...
    protected def helpFocusRoom(n:Room):Atlas;
    ...
  }

  private class BstAtlas(bst:Bst) extends Atlas {
    ...
    protected def helpFocusRoom(newRoom:Room):Atlas = ...
       // uses some of bst's methods
    ...
  }
}

但是当我去编译时,出现以下错误:

Question23.scala:15:错误:方法 无法访问 helpFocusRoom Atlas.阿特拉斯 a.helpFocusRoom(newRoom);

函数 helpFocusRoom 需要隐藏,但我不知道如何隐藏它并且仍然可以在伴随对象内部访问它。

谁能告诉我我在这里做错了什么?

I'm working on a homework assignment for my object oriented design class, and I'm running into trouble with Scala's companion objects. I've read in a few places that companion objects are supposed to have access to their companion class's private methods, but I can't seem to get it to work. (Just as a note, the meat of the assignment had to do with implementing a binary search tree, so I'm not just asking for answers...)

I have an object that is supposed to create an instance of my private class, BstAtlas (Bst is also defined in the Atlas object, took it out for clarity):

object Atlas {                                             
  def focusRoom(newRoom:Room,a:Atlas):Atlas = a.helpFocusRoom(newRoom);

  abstract class Atlas {
    ...
    protected def helpFocusRoom(n:Room):Atlas;
    ...
  }

  private class BstAtlas(bst:Bst) extends Atlas {
    ...
    protected def helpFocusRoom(newRoom:Room):Atlas = ...
       // uses some of bst's methods
    ...
  }
}

But when I go to compile, I get the following error:

Question23.scala:15: error: method
helpFocusRoom cannot be accessed in
Atlas.Atlas
a.helpFocusRoom(newRoom);

The function helpFocusRoom needs to be hidden, but I don't know how to hide it and still have access to it inside of the companion object.

Can anyone tell me what I'm doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-10-06 09:54:40

问题是类和伴生对象不能像这样嵌套。要定义伴生对象,您需要在对象主体之外但在同一文件中定义类。

The problem is that classes and companion objects can't be nested like that. To define a companion object, you need to define the class outside of the object's body but in the same file.

哽咽笑 2024-10-06 09:54:40

伴生对象应该位于其真实对象旁边,而不包含它:

object Example {
  class C(val i: Int = C.DefaultI) { }
  object C { protected val DefaultI = 5 }
}

scala> (new Example.C).i
res0: Int = 5

scala> Example.C.DefaultI
<console>:11: error: value DefaultI cannot be accessed in object Example.C
   Example.C.DefaultI

或者,您可以更改 protected 关键字的范围以包含封闭对象:

object Example {
  def value = (new D).hidden
  class D(val i: Int = 5) {
    protected[Example] def hidden = i*i
  }
}

scala> Example.value
res1: Int = 25

但在这里您不应该将外部对象命名为相同的名称作为内部类,否则您将很难从类内部引用它。

Companion objects should be next to their real object, not containing it:

object Example {
  class C(val i: Int = C.DefaultI) { }
  object C { protected val DefaultI = 5 }
}

scala> (new Example.C).i
res0: Int = 5

scala> Example.C.DefaultI
<console>:11: error: value DefaultI cannot be accessed in object Example.C
   Example.C.DefaultI

Alternatively, you can alter the scope of the protected keyword to include the enclosing object:

object Example {
  def value = (new D).hidden
  class D(val i: Int = 5) {
    protected[Example] def hidden = i*i
  }
}

scala> Example.value
res1: Int = 25

but here you ought not name the outer object the same thing as the inner class or you'll have trouble referring to it from within the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文