Scala 特性 - 是否有与 Java 接口公共静态字段等效的东西?
在 Java 中:
public interface Foo {
public static final int Bar = 0;
}
在 Scala 中,如何创建一个具有 Bar
的 trait Foo
,并且可以通过以下方式访问它:Foo.Bar
?
In Java:
public interface Foo {
public static final int Bar = 0;
}
And in Scala, how can I create a trait Foo
that has Bar
, and I can access it as: Foo.Bar
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个伴生对象(使其等效于 static)并使用 Final val 关键字定义变量(使其等效于 Final 常量):
更多内容 此处
You can create a companion object (to make it the equivalent of static) and define the variable there using the final val keywords (to make it the equivalent of a final constant):
Lots more on this here