scala覆盖java类方法

时间:2015-08-02 10:30:30

标签: java scala override

我有一个带有以下方法的java类:

class JavaContextObject {
...
public String[] getContext(int index, String[] tokens, String[] preds, Object[] additionalContext)
...

}

我希望在我的scala类中覆盖此方法 我尝试过以下签名:

override def getContext(index: Int, tokens: Array[String], preds: Array[String], additionalContext: Array[AnyRef]): Array[String] = ???

override def getContext(index: Int, tokens: Array[String], preds: Array[String], additionalContext: Array[Object]): Array[String] = ???

并收到以下我不理解的编译错误:

class ScalaContextObject needs to be abstract, since method getContext in trait BeamSearchContextGenerator of type (x$1: Int, x$2: Array[String], x$3: Array[String], x$4: Array[Object])Array[String] is not defined
(Note that Array[T with Object] does not match Array[String]: their type parameters differ)
  class ScalaContextObject {

其中BeamSearchContextGenerator - 是JavaContextObject已经实现的特性

public interface BeamSearchContextGenerator<T> {
  public String[] getContext(int index, T[] sequence, String[] priorDecisions, Object[] additionalContext);
}

我的问题是这个错误意味着什么以及如何避免它?

1 个答案:

答案 0 :(得分:0)

问题导致java不支持通用数组。使用T[]是不好的做法,所以如果可以用泛型类型替换它,例如List<T>