简体   繁体   English

Scala覆盖Java类方法

[英]scala override java class method

I have a java class with following method: 我有一个具有以下方法的Java类:

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

}

and I want to override this method in my scala class I have tried the following signatures: 并且我想在我的scala类中重写此方法,我尝试了以下签名:

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

or 要么

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

and receive the following compilation error that I dont understand: 并收到以下我不理解的编译错误:

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 {

where BeamSearchContextGenerator - is trait that JavaContextObject has already implemented 其中BeamSearchContextGenerator-是JavaContextObject已经实现的特征

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

My question is what this error means and how to avoid it ? 我的问题是此错误是什么意思,以及如何避免它?

Issue's caused that java doesn't support generic arrays. 问题导致Java不支持通用数组。 Using T[] is bad practice so if it's possible replace it with generic type, eg List<T> . 使用T[]是一种不好的做法,因此,如果有可能,请将其替换为通用类型,例如List<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM