加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 19:25:01 所属栏目:安全 来源:网络整理
导读:我有一个在 Java中定义的方法,如: void foo(int x,Thing... things) 我需要在Scala中覆盖它,但这两个都会给出错误: override def foo(x: Int,things: Thing*)override def foo(x: Int,things: Array[Thing]) 错误指的是重复...但我不知道那是什么. 更新 Ug
我有一个在 Java中定义的方法,如:

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

我需要在Scala中覆盖它,但这两个都会给出错误:

override def foo(x: Int,things: Thing*)
override def foo(x: Int,things: Array[Thing])

错误指的是<重复...>但我不知道那是什么.

更新

Ugg ……没关系.我在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的评论一致.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读