Scala 特性 - 是否有与 Java 接口公共静态字段等效的东西?

发布于 2024-12-27 10:39:27 字数 208 浏览 2 评论 0原文

在 Java 中:

public interface Foo {

  public static final int Bar = 0;
}

在 Scala 中,如何创建一个具有 Bartrait 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2025-01-03 10:39:27

您可以创建一个伴生对象(使其等效于 static)并使用 Final val 关键字定义变量(使其等效于 Final 常量):

trait Foo { }

object Foo { 
  final val Bar = 0
}

更多内容 此处

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):

trait Foo { }

object Foo { 
  final val Bar = 0
}

Lots more on this here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文