我在谷歌中搜索了case类和class之间的区别。每个人都提到,当你想在类上做模式匹配时,用例类。否则使用类,并提到一些额外的好处,如等号和哈希代码重写。但是这些就是为什么应该使用case类而不是类的唯一原因吗?

我想在Scala中应该有一些非常重要的原因。有什么解释,或者有资源可以学习更多关于Scala案例类的知识吗?


当前回答

Case类可以被看作是普通的、不可变的数据保存对象,应该完全依赖于它们的构造函数参数。

这个函数概念允许我们

使用紧凑的初始化语法(Node(1, Leaf(2), None))) 使用模式匹配分解它们 是否隐含地定义了相等比较

结合继承,case类被用来模拟代数数据类型。

如果一个对象在内部执行有状态计算或显示其他类型的复杂行为,那么它应该是一个普通类。

其他回答

Case类可以被看作是普通的、不可变的数据保存对象,应该完全依赖于它们的构造函数参数。

这个函数概念允许我们

使用紧凑的初始化语法(Node(1, Leaf(2), None))) 使用模式匹配分解它们 是否隐含地定义了相等比较

结合继承,case类被用来模拟代数数据类型。

如果一个对象在内部执行有状态计算或显示其他类型的复杂行为,那么它应该是一个普通类。

下面列出了case类的一些关键特性

Case类是不可变的。 可以实例化case类而不需要new关键字。 案例类可以根据值进行比较

scala fiddle的示例代码,摘自scala文档。

https://scalafiddle.io/sf/34XEQyE/0

从技术上讲,类和case类之间没有区别——即使编译器在使用case类时确实优化了一些东西。然而,一个case类用于消除特定模式的锅炉板,该模式正在实现代数数据类型。

这种类型的一个非常简单的例子是树。例如,二叉树可以这样实现:

sealed abstract class Tree
case class Node(left: Tree, right: Tree) extends Tree
case class Leaf[A](value: A) extends Tree
case object EmptyLeaf extends Tree

使我们能够做到以下几点:

// DSL-like assignment:
val treeA = Node(EmptyLeaf, Leaf(5))
val treeB = Node(Node(Leaf(2), Leaf(3)), Leaf(5))

// On Scala 2.8, modification through cloning:
val treeC = treeA.copy(left = treeB.left)

// Pretty printing:
println("Tree A: "+treeA)
println("Tree B: "+treeB)
println("Tree C: "+treeC)

// Comparison:
println("Tree A == Tree B: %s" format (treeA == treeB).toString)
println("Tree B == Tree C: %s" format (treeB == treeC).toString)

// Pattern matching:
treeA match {
  case Node(EmptyLeaf, right) => println("Can be reduced to "+right)
  case Node(left, EmptyLeaf) => println("Can be reduced to "+left)
  case _ => println(treeA+" cannot be reduced")
}

// Pattern matches can be safely done, because the compiler warns about
// non-exaustive matches:
def checkTree(t: Tree) = t match {
  case Node(EmptyLeaf, Node(left, right)) =>
  // case Node(EmptyLeaf, Leaf(el)) =>
  case Node(Node(left, right), EmptyLeaf) =>
  case Node(Leaf(el), EmptyLeaf) =>
  case Node(Node(l1, r1), Node(l2, r2)) =>
  case Node(Leaf(e1), Leaf(e2)) =>
  case Node(Node(left, right), Leaf(el)) =>
  case Node(Leaf(el), Node(left, right)) =>
  // case Node(EmptyLeaf, EmptyLeaf) =>
  case Leaf(el) =>
  case EmptyLeaf =>
}

注意,树的构造和解构(通过模式匹配)使用相同的语法,这也正是它们的打印方式(减去空格)。

它们也可以与哈希映射或集合一起使用,因为它们有一个有效、稳定的hashCode。

Scala中的case类构造也可以看作是删除一些样板文件的便利工具。

在构造一个case类时,Scala提供了以下内容。

它创建了一个类及其伴生对象 它的伴生对象实现了apply方法,您可以将其用作工厂方法。你不必使用new关键字,从而获得了语法上的优势。

因为类是不可变的,所以你得到了访问器,它只是类的变量(或属性),而没有突变器(因此没有改变变量的能力)。构造函数参数作为公共只读字段自动提供给您。比Java bean构造好用得多。

默认情况下还可以获得hashCode、equals和toString方法,equals方法从结构上比较对象。生成一个复制方法来克隆一个对象(其中一些字段具有提供给该方法的新值)。

正如前面提到的,最大的优点是可以在case类上进行模式匹配。这是因为您获得了unapply方法,该方法允许您分解case类以提取其字段。


实际上,在创建case类(或者case对象,如果你的类不带参数)时,你从Scala得到的是一个作为工厂和提取器的单例对象。

除了人们已经说过的,类和案例类之间还有一些更基本的区别

1.Case Class不需要显式的new,而Class需要用new调用

val classInst = new MyClass(...)  // For classes
val classInst = MyClass(..)       // For case class

2.默认情况下,构造函数的形参在类中是私有的,而在case类中是公共的

// For class
class MyClass(x:Int) { }
val classInst = new MyClass(10)

classInst.x   // FAILURE : can't access

// For caseClass
case class MyClass(x:Int) { }
val classInst = MyClass(10)

classInst.x   // SUCCESS

3.Case类根据值进行比较

// For Class
class MyClass(x:Int) { }
 
val classInst = new MyClass(10)
val classInst2 = new MyClass(10)

classInst == classInst2 // FALSE

// For Case Class
case class MyClass(x:Int) { }
 
val classInst = MyClass(10)
val classInst2 = MyClass(10)

classInst == classInst2 // TRUE