How to access the CALL_INDEX
object from java class:
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 }
}
In another scala file I can access the CALL_INDEX
using: CallStrategy.CALL_INDEX
But in java, I am not able to access it as follows:
StrategyUser.java
CallStrategy$ callStrategy = CallStrategy$.MODULE$;
MyStrategy$ myStrategy = MyStrategy$.MODULE$;
myStrategy.random(callStrategy); // compile time error
The above java code is giving compile time error:
The method random(CallStrategy) in the type MyStrategy$ is not applicable for the arguments (CallStrategy$)
I used the above java code by taking help from:
How do I "get" a Scala case object from Java?
UPDATE:
Please see the image below for clarification:
Even I added two more statements in the code and still the same compile time error:
CallStrategy callStrategy = CallStrategy$.MODULE$.CALL_INDEX$.MODULE$
您当前的代码根本没有提到CALL_INDEX
:它等同于Scala中的MyStrategy.random(CallStrategy)
。
You should do something like below:
MyStrategy$.MODULE$.random(CallStrategy.CALL_INDEX$.MODULE$);
In your code you are trying to access the CALL_INDEX from CallStrategy object, rather, it's available in CallStrategy interface.
Update 1: (Full Code)
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) = {
println(as)
}
}
StrategyUser.java:
public class StrategyUser {
public static void main(String[] args) {
MyStrategy$.MODULE$.random(CallStrategy.CALL_INDEX$.MODULE$);
}
}
And FYI, I am using scala version '2.11.7' and java version '1.7.0_75'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.