如何从 Java 调用具体的 Scala 特征方法? [英] How to invoke a concrete Scala trait method from Java?
问题描述
我有一个 Java/Scala 混合 Maven 项目.我需要重用 Saddle 方法 make
,该方法具体定义为名为 Index
的特征的一部分.该方法定义为 here 如果这有任何帮助.我曾尝试使用 Index.make
或 Index$class.make
从 java 调用该方法,但在这两种情况下,我都收到 error: cannot find symbol
编译错误.
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.
有没有办法从 Java 调用具体的 Trait 方法?
Is there a way to call a concrete Trait method from Java?
推荐答案
trait
类似于 Java 接口,它不是具体的构造函数.
A trait
is similar to a Java interface in a sense that it is not a concrete constructor.
来自斯卡拉:
class IndexImpl extends Index[SomeParamtersHere] {}
val x = new ClassImpl
x.make(..)
或者使用一个对象:
object Index extends Index {}
Index.make(..)
来自 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 {
}
这篇关于如何从 Java 调用具体的 Scala 特征方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!