问题
I have a Java / Scala mix Maven project. I need to reuse a Saddle method make
that is concretely defined as part of a trait called Index
. The method is defined here if that helps in any way. I have tried calling that method from java using Index.make
or Index$class.make
but in both cases I get the error: cannot find symbol
compilation error.
Is there a way to call a concrete Trait method from Java?
回答1:
A trait
is similar to a Java interface in a sense that it is not a concrete constructor.
From Scala:
class IndexImpl extends Index[SomeParamtersHere] {}
val x = new ClassImpl
x.make(..)
Or use an object:
object Index extends Index {}
Index.make(..)
From Java:
Traits with concrete implementations will compile to the appropriate abstract class
. If certain members don't have concrete implementation, you have to implement them.
import org.saddle.Index
public class TraitImpl extends Index$class {
}
回答2:
OK I found the answer. Calling it like this works but only because the Index trait already has an Index object available (that I didn't notice before):
Index$.MODULE$.make(rrule, startDate, endDate);
来源:https://stackoverflow.com/questions/22356442/how-to-invoke-a-concrete-scala-trait-method-from-java