scala 案例对象和对象的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5270752/
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
Difference between case object and object
提问by Wojciech Durczyński
Is there any difference between case object and object in scala?
scala 中的 case 对象和 object 之间有什么区别吗?
采纳答案by Dave Griffith
Case classes differ from regular classes in that they get:
案例类与常规类的不同之处在于它们获得:
- pattern matching support
- default implementations of
equalsandhashCode - default implementations of serialization
- a prettier default implementation of
toString, and - the small amount of functionality that they get from automatically inheriting from
scala.Product.
- 模式匹配支持
- 的默认实现
equals和hashCode - 序列化的默认实现
- 的更漂亮的默认实现
toString,以及 - 他们从自动继承的少量功能
scala.Product。
Pattern matching, equals and hashCode don't matter much for singletons (unless you do something really degenerate), so you're pretty much just getting serialization, a nice toString, and some methods you probably won't ever use.
模式匹配、equals 和 hashCode 对于单例并不重要(除非你做一些真正退化的事情),所以你几乎只是在获得序列化,一个很好的toString,以及一些你可能永远不会使用的方法。
回答by axel22
Here's one difference - case objects extend the Serializabletrait, so they can be serialized. Regular objects cannot by default:
这是一个区别 - case 对象扩展了Serializabletrait,因此它们可以被序列化。默认情况下,常规对象不能:
scala> object A
defined module A
scala> case object B
defined module B
scala> import java.io._
import java.io._
scala> val bos = new ByteArrayOutputStream
bos: java.io.ByteArrayOutputStream =
scala> val oos = new ObjectOutputStream(bos)
oos: java.io.ObjectOutputStream = java.io.ObjectOutputStream@e7da60
scala> oos.writeObject(B)
scala> oos.writeObject(A)
java.io.NotSerializableException: A$
回答by Minnie Shi
scala> object foo
defined object foo
定义对象 foo
scala> case object foocase
defined object foocase
定义对象 foocase
Serialization difference:
序列化差异:
scala> foo.asInstanceOf[Serializable]
java.lang.ClassCastException: foo$ cannot be cast to scala.Serializable
... 43 elided
java.lang.ClassCastException: foo$ 不能转换为 scala.Serializable
... 43 已省略
scala> foocase.asInstanceOf[Serializable]
res1: Serializable = foocase
res1:可序列化 = foocase
toString difference:
toString 区别:
scala> foo
res2: foo.type = foo$@7bf0bac8
res2: foo.type = foo$@7bf0bac8
scala> foocase
res3: foocase.type = foocase
res3: foocase.type = foocase
回答by Prashant_Sharma
case objects implicitly come with implementations of methods toString, equals, and hashCode, but simple objects don't. case objects can be serialized while simple objects cannot, which makes case objects very useful as messages with Akka-Remote. Adding the case keyword before object keyword makes the object serializable.
case 对象隐含地带有 toString、equals 和 hashCode 方法的实现,但简单对象没有。case 对象可以序列化,而简单对象不能,这使得 case 对象作为 Akka-Remote 的消息非常有用。在 object 关键字之前添加 case 关键字使对象可序列化。
回答by jeevan kishore
We know objects and "case class" before. But "case object" is a mix of both i.e it is a singleton similar to an object and with a lot of boilerplate as in a case class. The only difference is that the boilerplate is done for an object instead of a class.
我们之前知道对象和“案例类”。但是“案例对象”是两者的混合,即它是一个类似于对象的单例,并且与案例类一样具有很多样板。唯一的区别是样板是为对象而不是类完成的。
case objects won't come with the below ones:
case 对象不会带有以下对象:
Apply, Un-apply methods. here are no copy methods since this is a singleton. No method for structural equality comparison. No constructor as well.
应用,取消应用方法。这里没有复制方法,因为这是一个单例。没有结构平等比较的方法。也没有构造函数。
回答by wineway
It's similar with case classand class,we just use case objectinstead of case classwhen there isn't any fields representing additional state information.
它与case classand类似class,我们只是在没有任何表示附加状态信息的字段时使用case object而不是case class。

