从 Scala 调用受保护的静态 Java 方法

发布于 2024-10-07 19:17:35 字数 113 浏览 0 评论 0原文

我这里有一个库,里面有一些 Java 类。一个类有一些受保护的静态方法,我意识到这有点 OOP 禁忌,但我无法更改其代码。假设我有一个 Scala 类,它是上述 Java 类的子类,我如何调用它的受保护静态成员?

I have a library here with some Java classes. One class has some protected static methods, which I realize is sorta an OOP no-no but I can't change its code. Assuming I have a Scala class that subclasses the aforementioned Java class, how can I call its protected static members?

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

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

发布评论

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

评论(1

故事还在继续 2024-10-14 19:17:35

请参阅常见问题 - Java 互操作性

这是 Scala 的一个已知限制:
没有“静态”成员的概念
在斯卡拉。相反,Scala 对待
类 Y 的静态成员作为成员
单例对象 Y 的(
Y 类的伴生对象)。什么时候
从这个类继承,可以
仅访问类的受保护成员
Y 但无法访问受保护的成员
对象 Y 的。

Scala 无法模拟
静电保护而不损害
Scala 对象模型的完整性
根本方法,所以这不会发生
改变。要解决这个问题
限制,人们必须创建一个
封闭类的实现
用Java代码封装了所有
访问受保护的静态内部
类。

请参阅票号 #1806 了解更多信息
信息和具体例子
该限制及其解决方法

See Frequently Asked Questions - Java Interoperability:

This is a known limitation of Scala:
there is no notion of 'static' members
in Scala. Instead, Scala treats
static members of class Y as members
of the singleton object Y (the
companion object of class Y). When
inheriting from this class, one can
access only protected members of class
Y but cannot access protected members
of object Y.

There's no way Scala can simulate
static protected without impairing the
integrity of Scala's object model in a
fundamental way, so this is not going
to change. To work around this
limitation, one has to create an
implementation of the enclosing class
with Java code which encapsulates all
accesses to the protected static inner
class.

See ticket #1806 for more
information and a concrete example of
the limitation and its workaround.

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