我有一段Scala代码:
class A {
def main(args: Array[String])
}
然后是一段Java代码:
class B {
public static int test(final String... args) {
System.out.println("This is a test.");
// Use the arguments
...
}
}
我想要的是在A.main中调用B.test,如下所示:
class A {
def main(args: Array[String]) = {
val result = B.test(args)
}
}
上面的代码不起作用,因为类型不匹配。如果有人可以提供一个优雅的解决方案,将不胜感激。
请仅更改Scala部分,因为Java部分将来自另一个库。谢谢!
根据scala语言规范的4.6.2节,要(T... args)
在Java或(args: T*)
scala的情况下使用重复的参数将列表/数组提供给函数,则应通过_*
类型注释将参数标记为序列参数。
在这种特殊情况下B.test(args: _*)
将起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句