在Java中:
public interface Foo {
public static final int Bar = 0;
}
在Scala中,如何创建trait Foo
Bar
,我可以将其作为Foo.Bar
访问?
答案 0 :(得分:31)
您可以创建一个伴随对象(使其等效于静态)并使用最终的val关键字定义变量(使其等效于最终常量):
trait Foo { }
object Foo {
final val Bar = 0
}
有关此here
的更多信息