gpt4 book ai didi

scala - 从Scala调用 protected 静态Java方法

转载 作者:行者123 更新时间:2023-12-04 13:10:08 25 4
gpt4 key购买 nike

我这里有一些Java类的库。一类有一些protected static方法,我意识到这是一种OOP,不可以,但是我不能更改其代码。假设我有一个Scala类,该类继承了上述Java类,那么如何调用其 protected 静态成员呢?

最佳答案

参见 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.

关于scala - 从Scala调用 protected 静态Java方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4445617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com