简体   繁体   English

将带有通配符的方法覆盖从Java转换为Scala

[英]Convert method override with wildcard from Java to Scala

I have a Java method signature that I can't seem to convert to a signature in Java. 我有一个Java方法签名,似乎无法转换为Java中的签名。

Here is the Java code: 这是Java代码:

public class InjectorListCellRenderer extends DefaultListCellRenderer {
  public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    throw new RuntimeException("not important");
  }  
}

And here the Scala code which I hoped would be equivalent: 我希望这里的Scala代码是等效的:

class InjectorListCellRenderer(var painter: ParticleLabelPainter) extends DefaultListCellRenderer {
  override def getListCellRendererComponent(list: JList[_], value: Any, index: Int, isSelected: Boolean, cellHasFocus: Boolean): Component = {
    throw new RuntimeException("not important")
  }
}

However when compiling with sbt and Scala 2.11.8 I get the following error: 但是,使用sbt和Scala 2.11.8进行编译时,出现以下错误:

class InjectorListCellRenderer needs to be abstract, since method getListCellRendererComponent in trait ListCellRenderer of type (x$1: javax.swing.JList[_ <: Object], x$2: Object, x$3: Int, x$4: Boolean, x$5: Boolean)java.awt.Component is not defined InjectorListCellRenderer类必须是抽象的,因为特征类型ListCellRenderer中的getListCellRendererComponent类型为类型(x $ 1:javax.swing.JList [_ <:Object],x $ 2:Object,x $ 3:Int,x $ 4:Boolean,x $ 5:Boolean未定义java.awt.Component

The documentation for the base class DefaultListCellRenderer can be found here . 基类DefaultListCellRenderer的文档可以在这里找到。

I can't seem to reproduce this problem with my own code. 我似乎无法用自己的代码重现此问题。

Starting from the error message I would guess the following should work: 从错误消息开始,我猜测以下应该起作用:

class InjectorListCellRenderer(var painter: ParticleLabelPainter) extends DefaultListCellRenderer {
  override def getListCellRendererComponent(list: JList[_ <: AnyRef], value: AnyRef, index: Int, isSelected: Boolean, cellHasFocus: Boolean): Component = {
    throw new RuntimeException("not important")
  }
}

Edit: after some experimentation I think that inheriting from DefaultListCellRenderer and overriding getListCellRendererComponent is not possible due to some inconsistency between Java's and Scala's type representations. 编辑:经过一些试验,我认为由于Java和Scala的类型表示形式之间存在一些不一致,因此无法从DefaultListCellRenderer继承并覆盖getListCellRendererComponent是不可能的。 If this is important to you you might consider filing a bug report. 如果这对您很重要,则可以考虑提交错误报告。

Its saying the type of the JList argument isnt the same because you've left it as a hole [_] but it expects at least object. 它说的JList参数的类型是不一样的,因为您将其保留为孔[_]但它至少需要对象。

Seems to work if you put object , any or make it parametric with T . 如果放置objectany object ,或者使其与T参数化,则似乎可以正常工作。

class InjectorListCellRenderer() extends DefaultListCellRenderer{
   def getListCellRendererComponent(list: JList[Object], value: Any, index: Int, isSelected: Boolean, cellHasFocus: Boolean) = ???
}

or 要么

class InjectorListCellRenderer() extends DefaultListCellRenderer{
   def getListCellRendererComponent[T](list: JList[T], value: Any, index: Int, isSelected: Boolean, cellHasFocus: Boolean) = ???
}

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

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