【发布时间】:2016-01-04 18:38:00
【问题描述】:
如何从java类访问CALL_INDEX对象:
MyStrategy.scala
trait CallStrategy
object CallStrategy {
case object CALL_INDEX extends CallStrategy
}
trait MyStrategy { def random(as: CallStrategy) }
object MyStrategy extends MyStrategy
{
def random(as: CallStrategy) = { //Some code }
}
在另一个 scala 文件中,我可以使用以下命令访问 CALL_INDEX:
CallStrategy.CALL_INDEX
但在java中,我无法按如下方式访问它:
StrategyUser.java
CallStrategy$ callStrategy = CallStrategy$.MODULE$;
MyStrategy$ myStrategy = MyStrategy$.MODULE$;
myStrategy.random(callStrategy); // compile time error
上面的java代码给出编译时错误:
The method random(CallStrategy) in the type MyStrategy$ is not applicable for the arguments (CallStrategy$)
我通过以下方式使用了上面的 java 代码:
How do I "get" a Scala case object from Java?
更新:
请看下图进行说明:
即使我在代码中又添加了两条语句,但仍然出现相同的编译时错误:
【问题讨论】: