Scala:类中的公共静态最终
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1632138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala: public static final in a class
提问by renfis
I'm trying to get a real equivalent for Java's public static finalin Scala for using TwiP.
我正在尝试public static final在 Scala 中获得Java 的真正等价物以使用TwiP。
Creating a valin an objectdoesn't work for me, because it's part of a new generated class Example$.classand TwiP can't access it from class Example.class.
val在 anobject中创建 a对我不起作用,因为它是新生成的类的一部分,Example$.class而 TwiP 无法从 class 访问它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 finalto Scala. If it's a val in an object like here
但我不知道如何将 移植public static final到 Scala。如果它是像这里这样的对象中的 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 解决问题?
采纳答案by Ken Bloom
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 #1735and 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 与非静态参数生成方法一起工作。
回答by Ken Bloom
object Foo{
val MY_STRINGS=Array("A","B","C")
}
class Foo{
import Foo.MY_STRINGS
}
The valdefinition in the companion object creates your public static finalvariable, and the importdeclaration 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 finalvariable in Scala still will compile to look like a static method call if you call this code from Java.
请注意,public static final如果您从 Java 调用此代码,Scala中的变量仍将编译为看起来像静态方法调用。
Edit:I'm slightly wrong because of a bug in Scala 2.7, which I demonstrate in detail in another answer.
编辑:由于 Scala 2.7 中的一个错误,我有点错误,我在另一个答案中详细说明了这一点。
回答by Marcos
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. Take a look at: http://docs.scala-lang.org/style/naming-conventions.html
注意:scala 中的最终静态变量与 Java 中的约定不同。看看:http: //docs.scala-lang.org/style/naming-conventions.html
回答by James Black
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 finalon a variable, as a better solution might be more obvious then.
这可能不是最好的方法,但如果您能解释为什么要public static final在变量上使用,那将会很有帮助,因为那样更好的解决方案可能会更明显。

