4

我有一个用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只是缺少一个主体,但错误表明问题是可变参数的类型。

4

1 回答 1

1

爪哇:

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 的评论一致。

于 2013-01-15T23:50:35.570 回答