简体   繁体   English

Scala中Case类的理解

[英]Understanding of Case class in Scala

I have below code but when i remove case statement , object instance(Donut) cannot able to refer .我有以下代码,但是当我删除 case 语句时,对象实例(甜甜圈)无法引用。 I want to understand with simple example with class and case class statements , Please advise .我想用类和案例类声明的简单例子来理解,请指教。 Also i want to understand why '(' ')' is printed in my second print statement.我也想了解为什么在我的第二个打印语句中打印 '(' ')' 。

 case class Donut(name: String, tasteLevel: String)

   val favoriteDonut2: Donut = Donut("Glazed Donut", "Very Tasty")
   println(s"My favorite donut name = ${favoriteDonut2.name}, 
          tasteLevel =  ${favoriteDonut2.tasteLevel}")
   println( s"My fav donut name is = ${favoriteDonut2.name}",
            s"taste level is       = ${favoriteDonut2.tasteLevel}")

output:-
   My favorite donut name = Glazed Donut, tasteLevel = Very Tasty
   (My fav donut name is = Glazed Donut,taste level is     = Very Tasty)

When you call Donut("Glazed Donut", "Very Tasty") you're really calling the apply method of the Donut object .当您调用Donut("Glazed Donut", "Very Tasty")您实际上是在调用Donut对象apply方法
When you mark a class as a case class , scala will automatically generate a bunch of things for you, between them a companion object for your class with an apply method that takes all the arguments you define.当你将一个类标记为case class ,scala 会自动为你生成一堆东西,在它们之间为你的类生成一个伴生对象,其中一个 apply 方法接受你定义的所有参数。
That's why if you define it as just a normal class, the line will fail - you can call new Donut("Glazed Donut", "Very Tasty") instead.这就是为什么如果您将其定义为普通类,该行将失败 - 您可以改为调用new Donut("Glazed Donut", "Very Tasty") Or you could create the apply method by hand.或者您可以手动创建 apply 方法。

class Donut(val name: String, val tasteLevel: String) // vals needed to make the fields public, case classes do this by default.
object Donut {
  /** Factory of Donuts */
  def apply(name: String, tasteLevel: String): Donut = new Donut(name, tasteLevel)
}

Note笔记

Case Classes provide another bunch of useful functionalities appart of a simple constructor, like: pretty toString implementation, good hashCode implementation, "by value" comparison, simple "copying" , extractors for pattern matching , etc - that remove "boilerplate" from the code. 案例类提供了简单构造函数的另一组有用功能,例如:漂亮的toString实现、良好的hashCode实现、 “按值”比较、简单的“复制”模式匹配提取器- 从代码中删除“样板” .
Thus if you need a couple of these features and the class is not intended to be mutated, prefer a case class than implementing everything by yourself.因此,如果您需要这些功能中的几个,并且该类不打算进行变异,那么宁愿使用case 类而不是自己实现所有内容。

" Also i want to understand why '(' ')' is printed in my second print statement." “我还想了解为什么在我的第二个打印语句中打印了 '(' ')'。”

Scala's print function, only receives one argument. Scala 的print函数,只接收一个参数。 Thus, what happens when you write print(a, b) is that you call print with the tuple (a, b) - (which is the same as calling print((a, b)) ) .因此,当您编写print(a, b)时会发生什么是您使用元组(a, b) - (这与调用print((a, b)) )一起调用print((a, b))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM