从Scala调用受保护的静态Java方法 [英] Calling a protected static Java method from Scala

查看:152
本文介绍了从Scala调用受保护的静态Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个带有一些Java类的库.一个类有一些protected static方法,我意识到这是一种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?

推荐答案

请参见 常见问题解答问题-Java互操作性 :

See Frequently Asked Questions - Java Interoperability:

这是Scala的已知限制: 没有静态"成员的概念 在斯卡拉.相反,Scala对待 Y类的静态成员作为成员 单例对象Y的( Y类的随播对象).什么时候 从这个类继承,可以 仅访问受保护的班级成员 是,但无法访问受保护的成员 物体Y.

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.

Scala无法模拟 静电保护而不会损害 Scala对象模型的完整性 基本的方式,所以这不会 改变.要解决此问题 局限性,必须创建一个 封闭类的实现 带有封装所有代码的Java代码 访问受保护的静态内部 课.

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.

有关更多信息,请参见机票#1806 . 信息和一个具体的例子 限制及其解决方法.

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

这篇关于从Scala调用受保护的静态Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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