简体   繁体   English

案例 class 和伴侣 object

[英]Case class and companion object

I have problems understanding correctly the use of class and their companion object.我无法正确理解 class 及其同伴 object 的使用。

When defining a case class there is its companion object that comes with it, but what is the result of defining an object having the same name as the case class?当定义一个案例 class 时,它附带了它的同伴 object,但是定义一个与案例 class 同名的 object 的结果是什么? Does it override the companion object?它会覆盖同伴 object 吗? And how to access case class parameters?以及如何访问 case class 参数?

For example in TestCaseClass.scala file I define the following:例如,在 TestCaseClass.scala 文件中,我定义了以下内容:

case class TestCaseClass(att1: String, att2: Int, att4s: List[String])

object TestCaseClass {

  def iWantDoSomethingWithMyParams: String = {
    att1 + " " + att2
  }

  // Other functions
}

object AnotherTestCaseClass {

  def iWantDoSomethingWithTestCaseClassParams: String = {
  // How to access TestCaseClass.att1
    TestCaseClass.att1 + " " + TestCaseClass.att2
  }

  def iWantGetAllAttr4: List[String] = {
     // ???
  }
}

To some extent, giving an object the same name as a class (or trait ) is just a matter of convention. 在某种程度上,给一个与class (或trait )同名的object只是一个惯例问题。 But it also has a bit of special meaning. 但它也有一些特殊的含义。

The companion object is a singleton class just like any other object. 伴随对象是一个单独的类,就像任何其他对象一样。 If you want a method in the companion object to interact with an instance of the class, you have to pass it an instance of the class just like in any other situation. 如果您希望伴随对象中的方法与类的实例进行交互,则必须像在任何其他情况下一样传递该类的实例。 So, to fix your first example: 所以,要修复你的第一个例子:

case class TestCaseClass(att1: String, att2: Int, att4s: List[String])

object TestCaseClass {    
  def iWantDoSomethingWithMyParams(x: TestCaseClass): String =
    x.att1 + " " + x.att2
}

The class and the object do not "override" or step on each other's toes in any way because classes and objects belong to different namespaces. 类和对象不会以任何方式“覆盖”或踩到彼此的脚趾,因为类和对象属于不同的命名空间。 Class names are used at the type level (and also in constructor calls), and object names are used at the term level. 类名称在类型级别(以及构造函数调用)中使用,对象名称在术语级别使用。

There are a few relationships between a class and its companion: 类及其伴侣之间存在一些关系:

  • It does affect how implicits are resolved - Any implicts defined in a class's companion object are automatically brought into scope. 它确实会影响implicits的解析方式 - 类的伴随对象中定义的任何隐含都会自动进入范围。

  • private members of the class are visible to the object, and vice versa. 该类的private成员对该对象可见,反之亦然。

  • Case classes are a little bit different, because case class is actually a shorthand which, in addition to defining a class, also adds apply and unapply methods to its companion object. 案例类有点不同,因为case class实际上是一种简写,除了定义类之外,还会向其伴随对象添加applyunapply方法。

I would kindly like to add one piece of information to the accepted answer that wasn't clear to me after reading it a couple of times;我想在接受的答案中添加一条信息,在阅读几次后我还不清楚; it's from the Scala Book ( all credits to mlachkar, 0x54321 and alvinj ):它来自Scala Book所有学分归于 mlachkar、0x54321 和 alvinj ):

A companion object in Scala is an object that's declared in the same file as a class, and has the same name as the class . Scala 中的同伴 object 是一个 object,它在与 class 相同的文件中声明,并且与 class 具有相同的名称 For instance, when the following code is saved in a file named Pizza.scala , the Pizza object is considered to be a companion object to the Pizza class:例如,当以下代码保存在名为Pizza.scala的文件中时,Pizza object 被认为是 Pizza class 的同伴 object:

 class Pizza { } object Pizza { }

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

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