gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:25:08 27 4
gpt4 key购买 nike

我有一个在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com