Scala:班级中的公共静态决赛

编程入门 行业动态 更新时间:2024-10-25 12:29:23
本文介绍了Scala:班级中的公共静态决赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 public static final 的真正等价物.htmlrel =nofollow noreferrer> TwiP 。

I'm trying to get a real equivalent for Java's public static final in Scala for using TwiP.

在一个<创建一个 val code> object 对我不起作用,因为它是新生成的类的一部分示例$ .class 并且TwiP不能从类 Example.class 访问它。

Creating a val in an object doesn't work for me, because it's part of a new generated class Example$.class and TwiP can't access it from class Example.class.

以下是我尝试移植到的Java类的示例Scala:

Here's an example of a Java Class I'm trying to port to Scala:

public static final String[] MY_STRINGS = { "A", "B", "C" }; @Test public void myTest(@Values("MY_STRINGS") String string) { ... }

但我不知道如何将 public static final 移植到Scala。如果它是像这里的对象中的val那么

But I don't know how to port the public static final to Scala. If it's a val in an object like here

@RunWith(classOf[TwiP]) class Foo { import Foo.MY_STRINGS @Test def testTwiP(@Values("MY_STRINGS") value: String): Unit = { println("I'm testing value " + value + ".") } } object Foo { val MY_STRINGS = Array("A", "B", "C") }

我只得到以下异常:

net.sf.twip.internal.TwipConfigurationError: there is no method or field 'MY_STRINGS' named in the @Values annotation of Parameter#1

如何使用Scala解决问题?

How can I solve the problem using Scala?

推荐答案

以下Scala代码:

class Foo{ import Bar.MY_STRINGS } object Bar{ val MY_STRINGS=Array("A","B","C") }

生成以下Java类:

public final class Bar extends java.lang.Object{ public static final java.lang.String[] MY_STRINGS(); public static final int $tag() throws java.rmi.RemoteException; } public final class Bar$ extends java.lang.Object implements scala.ScalaObject{ public static final Bar$ MODULE$; public static {}; public Bar$(); public java.lang.String[] MY_STRINGS(); public int $tag() throws java.rmi.RemoteException; } public class Foo extends java.lang.Object implements scala.ScalaObject{ public Foo(); public int $tag() throws java.rmi.RemoteException; }

以下Scala代码:

class Foo{ import Foo.MY_STRINGS } object Foo{ val MY_STRINGS=Array("A","B","C") }

生成以下Java类:

public class Foo extends java.lang.Object implements scala.ScalaObject{ public Foo(); public int $tag() throws java.rmi.RemoteException; } public final class Foo$ extends java.lang.Object implements scala.ScalaObject{ public static final Foo$ MODULE$; public static {}; public Foo$(); public java.lang.String[] MY_STRINGS(); public int $tag() throws java.rmi.RemoteException; }

当对象具有静态成员时,未在类上定义静态成员与该类相同的名称是 Scala Bug#1735 ,它已在Scala中修复2.8快照。

The fact that static members aren't defined on the class when the object has the same name as the class is Scala Bug #1735 and it's fixed in Scala 2.8 snapshots.

所以看起来TwiP根本无法工作,除非你要么升级Scala,要么找到让TwiP与非静态工作的方法参数生成方法。

So it looks like TwiP isn't going to work at all unless you either upgrade Scala, or find a way to get TwiP to work with non-Static parameter generation methods.

更多推荐

Scala:班级中的公共静态决赛

本文发布于:2023-07-15 10:18:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   班级   决赛   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!