scala 对象作为字段

发布于 2024-10-06 09:26:46 字数 442 浏览 0 评论 0原文

可能的重复:
scala 类中的 val 和 object?

之间有本质区别吗 ? :

class Foo {
  object timestamp extends java.util.Date
}

拥有一个带有对象字段的类到底

class Foo {
  val timestamp = new java.util.Date {}
}

意味着什么?它们有什么用?是否存在必须使用对象的情况?

谢谢...

Possible Duplicate:
val and object inside a scala class?

Is there a substantive difference between:

class Foo {
  object timestamp extends java.util.Date
}

and

class Foo {
  val timestamp = new java.util.Date {}
}

What does it really mean to have a class with an object field? What are they used for? Are there situations where you must use an object?

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

慵挽 2024-10-13 09:26:46

如果您需要向字段添加行为,那么使用对象可能会更好。例如:

class Foo {
   object startDate extends java.util.Date {
      def isBusinessDay: Boolean = // ...
   }
}

class Bar {
   lazy val startDate = new java.util.Date {
      def isBusinessDay: Boolean = // ...
   }
}

foo.startDate的类型是foo.startDate.type,调用foo.startDate.isBusinessDay方法将是静态解析。

另一方面,bar.startDate 的类型是结构类型java.util.Date{ def isBusinessDay: Boolean }。因此,调用 bar.startDate.isBusinessDay 将使用反射并产生不必要的运行时开销。

Using an object may be preferable if you need to add behavior to the field. For example:

class Foo {
   object startDate extends java.util.Date {
      def isBusinessDay: Boolean = // ...
   }
}

class Bar {
   lazy val startDate = new java.util.Date {
      def isBusinessDay: Boolean = // ...
   }
}

The type of foo.startDate is foo.startDate.type, and a call to the foo.startDate.isBusinessDay method will be resolved statically.

The type of bar.startDate, on the other hand, is the structural type java.util.Date{ def isBusinessDay: Boolean }. A call to bar.startDate.isBusinessDay will therefore use reflection and incur unnecessary runtime overhead.

初相遇 2024-10-13 09:26:46

有两个区别,一是重要的,一是微妙的。首先,对象是延迟启动的。出于您的目的,这意味着您的第二个示例应该是

class Foo {
  lazy val timestamp = new java.util.Date {}
}

让它们表现相同。更巧妙的是,每个对象都有自己独特的类型。在您的示例中,timestamp 将具有类型 Foo.timestamp.type。这在实践中通常并不重要,但如果您以反射方式访问对象,则可能会导致意外。

Two differences, one important, one subtle. First, objects are lazily initiated. For your purposes, this means your second example should be

class Foo {
  lazy val timestamp = new java.util.Date {}
}

for them to behave equivalently. More subtly, each object has it's own unique type. In the case of your example, timestamp will have type Foo.timestamp.type. This normally isn't important in practice, but may cause surprises if you are accessing objects reflectively.

不再让梦枯萎 2024-10-13 09:26:46

它们的行为方式基本相同。我曾假设 class X { object Y ... } 只会为 XY 提供一个命名空间。我使用 Y 来存储与 X 相关的(常量)实用程序数据,具体是因为我期望有一个唯一的 XY。事实上,我在进行内存分析时发现的每一个新 X 都会构造一个新对象。

Those do behave basically the same way. I had assumed that class X { object Y ... } would simply provide a namespace for X.Y. I used Y to store (constant) utility data related to X specifically because I expected one unique X.Y. In fact, one new object was constructed for every new X which I discovered when doing memory profiling.

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