scala 案例类和案例对象之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32078526/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 07:28:51  来源:igfitidea点击:

Difference between case class and case object?

scalaakka

提问by daydreamer

I am learning Scala and Akka and in my recent lookup for a solution, I found something like

我正在学习 Scala 和 Akka,在最近寻找解决方案时,我发现了类似的东西

 case class TotalTaxResult(taxAmount:Double)
 case object TaxCalculationTimeout

What is the difference between the two?

两者有什么区别?

When should I use one over the other?

我什么时候应该使用一个?

回答by cmbaxter

A case class can take arguments, so each instance of that case class can be different based on the values of it's arguments. A case object on the other hand does not take args in the constructor, so there can only be one instance of it (a singleton, like a regular scala objectis).

一个案例类可以接受参数,因此该案例类的每个实例都可以根据其参数的值而不同。另一方面,case 对象在构造函数中不接受 args,因此它只能有一个实例(单例,就像常规 scala 一样object)。

If your message to your actor does not need any value differentiation, use a case object. For instance, if you had an actor that did some work, and you, from the outside, wanted to tell it to do work, then maybe you'd do something like this:

如果您向演员传达的信息不需要任何价值区分,请使用案例对象。例如,如果你有一个演员做了一些工作,而你从外面想告诉它工作,那么也许你会做这样的事情:

case object DoWork

...

def receive = {
  case DoWork => 
     //do some work here
}

But if you wanted some variation in how the work is done, you might need to redefine your message like so:

但是如果你想改变工作的完成方式,你可能需要像这样重新定义你的消息:

case class DoWorkAfter(waitTime:Long)

...

def receive = {
  case class DoWorkAfter(time) =>
    context.system.scheduler.scheduleOnce(time.milliseconds, self, DoWork)

  case DoWork => 
     //do some work here
}

回答by gregghz

A case object is a singleton case class. They are used kind of like enumeration values. It can be used in pattern matching just like any other value:

case 对象是一个单例 case 类。它们的使用有点像枚举值。它可以像任何其他值一样用于模式匹配:

TaxCalculationTimeout match {
  case TaxCalculationTimeout => println("hello")
}

When you define a case class, you are creating a template for instances of that class. TotalTaxResult(1.0)and TotalTaxResult(2.0)are two different values of the same type. Whereas there is exactly one TaxCalculationTimeoutvalue.

定义案例类时,您正在为该类的实例创建模板。TotalTaxResult(1.0)并且TotalTaxResult(2.0)是相同类型的两个不同值。而只有一个TaxCalculationTimeout值。

回答by Harmeet Singh Taara

In simple words, Scala is a Object Oriented and Functional programming language. It have features of functional programming like pattern matching with pure object oriented methodology.

简单来说,Scala 是一种面向对象的函数式编程语言。它具有函数式编程的特点,例如使用纯面向对象方法进行模式匹配。

Some times, we need to create singleton object without any value like passing some signal for pattern matching. If scala have not concept of case object we just to need to use enum or equals some string value in matching. But this is not a readability in pure Object Oriented language.. In that scenario we are using Case Object

有时,我们需要创建没有任何值的单例对象,例如传递一些信号进行模式匹配。如果scala没有case对象的概念,我们只需要在匹配中使用枚举或等于某个字符串值。但这不是纯面向对象语言的可读性。在这种情况下,我们使用 Case Object

Case classes are used when we need to create multiple objects with different values.

当我们需要创建具有不同值的多个对象时,会使用案例类。

回答by Prashant_Sharma

You can think of these two to be just like a class and an object in general. When you do a case class ClassName(params list) it creates a blueprint for making objects and case object defines a singleton object in the scope in which it is declared. Starting with Scala 2.10, you should always use case objects instead of case classes with no arguments. So when you want to do a pattern match on some values which need arguments you should go for a case class, but if your values don't take arguments then you should use a case object.

您可以将这两者视为一般的类和对象。当您执行案例类 ClassName(params list) 时,它会创建一个用于制作对象的蓝图,并且案例对象在声明它的范围内定义了一个单例对象。从 Scala 2.10 开始,您应该始终使用 case 对象而不是没有参数的 case 类。因此,当您想对某些需要参数的值进行模式匹配时,您应该使用 case 类,但如果您的值不带参数,则应使用 case 对象。