开发者

Calling a protected static Java method from Scala

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 aforem开发者_StackOverflowentioned Java class, how can I call its protected static members?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜