简体   繁体   中英

scala override java class method

I have a java class with following method:

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:

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

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. Using T[] is bad practice so if it's possible replace it with generic type, eg List<T> .

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.

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