简体   繁体   English

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

[英]Scala: public static final in a class

I'm trying to get a real equivalent for Java's public static final in Scala for using TwiP . 我试图在Scala中使用TwiP获得Java的public static final 版本

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 . object中创建val对我来说不起作用,因为它是新生成的类的一部分。 Example$.class和TwiP无法从类Example.class访问它。

Here's an example of a Java Class I'm trying to port to Scala: 这是我尝试移植到Scala的Java类的示例:

public static final String[] MY_STRINGS = { "A", "B", "C" };

@Test
public void myTest(@Values("MY_STRINGS") String string) {
  ...
}

But I don't know how to port the public static final to Scala. 但我不知道如何将public static final移植到Scala。 If it's a val in an object like here 如果它是像这里的对象中的val

@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")
}

I only get the following exception: 我只得到以下异常:

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

How can I solve the problem using Scala? 如何使用Scala解决问题?

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

The val definition in the companion object creates your public static final variable, and the import declaration gives it a nice easy alias in the code you're using to write the class. 伴随对象中的val定义创建了您的public static final变量,并且import声明在您用于编写类的代码中为它提供了一个很好的简单别名。

Note that the public static final variable in Scala still will compile to look like a static method call if you call this code from Java. 请注意,如果从Java调用此代码,Scala中的public static final变量仍将编译为看起来像静态方法调用。

Edit: I'm slightly wrong because of a bug in Scala 2.7, which I demonstrate in detail in another answer. 编辑:由于Scala 2.7中的错误,我有点错误,我在另一个答案中详细说明了这个错误。

The following Scala code: 以下Scala代码:

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

Generates the following Java classes: 生成以下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;
}

The following Scala code: 以下Scala代码:

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

Generates the following Java classes: 生成以下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;
}

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. 当对象与类同名时,静态成员未在类上定义的事实是Scala Bug#1735并且它已在Scala 2.8快照中修复。

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. 所以看起来TwiP根本无法工作,除非您升级Scala,或者找到一种方法让TwiP与非静态参数生成方法一起使用。

You just have to define the variable as "val" in a companion object. 您只需在伴随对象中将变量定义为“val”。

object Foo{
  val MyStrings = Array("A","B","C")
}

NOTE: final static variables in scala doesn't follow the same convention as in Java. 注意:scala中的最终静态变量不遵循与Java中相同的约定。 Take a look at: http://docs.scala-lang.org/style/naming-conventions.html 请查看: http//docs.scala-lang.org/style/naming-conventions.html

If you use a var then you can create your own getter and setter, and if the value is already set, don't change it. 如果使用var,则可以创建自己的getter和setter,如果已经设置了值,则不要更改它。

That may not be the best approach, but it would be helpful if you could explain why you want to use public static final on a variable, as a better solution might be more obvious then. 这可能不是最好的方法,但是如果你能解释为什么要在变量上使用public static final会有所帮助,因为更好的解决方案可能会更加明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM