简体   繁体   English

在Scala中使用附带对象扩展案例类

[英]Extending a case class with companion object in scala

I have 2 case classes with respective companion objects in 2 different files. 我在2个不同的文件中有2个带有各自伴随对象的案例类。 I need to extend one of these classes and have the other class in the same file. 我需要扩展这些类之一,并在同一文件中具有另一个类。 Is this possible in scala? 在scala中这可能吗? Any pointers on how to go about it? 关于如何进行操作的任何指示?

Following are the existing classes A & B in files A.scala and B.scala 以下是文件A.scala和B.scala中现有的类A和B

object A {
  def apply(){}
}
case class A(a: Int, b: Int) {}

object B {
  def apply() {}
}
case class B(x: Int, y: Int) {}

I need a 3rd class C which extends A and also has class B in a 3rd file C.scala. 我需要一个扩展A的第三类C,并且在第三文件C.scala中也具有B类。

1) You shouldn't inherit from a case class 1)您不应该继承案例类

2) It is possible to declare two classes with the same name if they full names (including the package names) are different. 2)如果两个全名(包括程序包名称)不同,则可以用相同的名称声明两个类。 So file C.scala must belong to a different package. 因此,文件C.scala必须属于其他程序包。

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

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