scala - 如何在 Scala 中覆盖 Java varargs 方法?

标签 scala variadic-functions scala-java-interop

我有一个在 Java 中定义的方法,例如:

void foo(int x, Thing... things)

我需要在 Scala 中覆盖它,但是这两个都会出现错误:
override def foo(x: Int, things: Thing*)
override def foo(x: Int, things: Array[Thing])

错误引用 <repeated...>但我不知道那是什么。

更新

呃……没关系。我在 2.10.0 中,我输入错误并且没有方法主体。然后我对这个错误消息感到困惑,这对我来说仍然很奇怪。在 SBT 中:
> compile
[info] Compiling 1 Scala source to [...]/target/scala-2.10/classes...
[error] [...]/src/main/scala/Translator.scala:41: class MyMethodVisitor needs to be abstract, since method visitTableSwitchInsn is not defined
[error] (Note that org.objectweb.asm.Label* does not match <repeated...>[org.objectweb.asm.Label])
[error]   class MyMethodVisitor extends MethodVisitor (Opcodes.ASM4) {
[error]         ^

问题是我的 visitTableSwitchInsn只是缺少主体,但错误表明问题在于 varargs 参数的类型。

最佳答案

java :

package rrs.scribble;

public
class   VA1
{
    public int va1(int... ints) {
        return ints.length;
    }
}

斯卡拉:
package rrs.scribble

class   VA1S
extends VA1
{
  override
  def va1(ints: Int*): Int =
    ints.length * 2
}

SBT:
> ~compile
[info] Compiling 1 Scala source and 1 Java source to …/scribble/target/scala-2.10/classes...
[success] Total time: 4 s, completed Jan 15, 2013 3:48:14 PM
1. Waiting for source changes... (press enter to interrupt)

这是 Scala 2.10,与@TravisBrown 的评论一致。

关于scala - 如何在 Scala 中覆盖 Java varargs 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14348631/

相关文章:

scala - 并行遍历文件中的行(Scala)?

c - 通过带有变量名称的宏进行调试打印

c++ - 如何调用所有可变继承类的函数?

java - 我可以直接在Java中使用scala List吗?

oop - 具有伴随对象的类与具有相同名称的类和对象之间有什么区别?

java - 将 scala.collection.iterator 对象放入 scala 类构造函数中

java - Groovy、Scala 可能让我的生活更轻松?

scala - 在 SCALA 中查找通用类型的第 N 个元素抛出 NoSuchElementException

c++ - 使用可变参数模板的随机选择器函数——这可能吗?

scala - 使用 Akka IO 通过 Tcp 客户端发送非 ByteString