案例对象和对象的区别 [英] Difference between case object and object

查看:40
本文介绍了案例对象和对象的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scala 中 case 对象和对象有什么区别吗?

Is there any difference between case object and object in scala?

推荐答案

案例类与常规类的不同之处在于:

Case classes differ from regular classes in that they get:

  1. 模式匹配支持
  2. equalshashCode 的默认实现
  3. 序列化的默认实现
  4. toString 的更漂亮的默认实现,以及
  5. 他们从scala.Product 自动继承的少量功能.
  1. pattern matching support
  2. default implementations of equals and hashCode
  3. default implementations of serialization
  4. a prettier default implementation of toString, and
  5. the small amount of functionality that they get from automatically inheriting from scala.Product.

模式匹配、equals 和 hashCode 对单例并不重要(除非你做一些真正退化的事情),所以你几乎只是获得序列化,一个很好的 toString,以及一些你需要的方法可能永远不会用了.

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.

这篇关于案例对象和对象的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆