必须在 Scala 中覆盖 val 变量

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

Must override val variable in scala

scala

提问by zjffdu

I meet a weird problem in scala. Following is my code, class Employee extends class Person

我在 Scala 中遇到了一个奇怪的问题。以下是我的代码,类 Employee 扩展类 Person

But this piece of code can not been compiled, I have explicit define firstName and lastName as val variable. Why is that ? Does it mean I have to override val variable in base class ? And what is the purpose ?

但是这段代码无法编译,我已经明确定义了 firstName 和 lastName 作为 val 变量。这是为什么 ?这是否意味着我必须覆盖基类中的 val 变量?目的是什么?

class Person( firstName: String,  lastName: String) {

}

class Employee(override val firstName: String, override val lastName: String, val depart: String)
    extends Person(firstName,lastName){

} 

回答by Rex Kerr

The input parameters for the constructor are not vals unless you say they are. And if they are already, why override them?

构造函数的输入参数不是 val,除非你说它们是。如果它们已经存在,为什么要覆盖它们?

class Person(val firstName: String, val lastName: String) {}
class Strange(
  override val firstName: String, override val lastName: String
) extends Person("John","Doe") {}
class Employee(fn: String, ln: String, val depart: String) extends Person(fn,ln) {}

If they're not vals and you want to make vals, you don't need to override:

如果它们不是 vals 并且您想创建 vals,则不需要覆盖:

class Person(firstName: String, lastName: String) {}
class Employee(
  val firstName: String, val lastName: String, val depart: String
) extends Person(firstName,lastName) {}

回答by Dirk

Since the constructor arguments have no val/var declaration in Person, and as Person is no case class, the arguments will not be members of class Person, merely constructor arguments. The compiler is telling you essentially: hey, you said, that firstName and lastName are members, which override/redefine something inherited from a base class - but there is nothing as far as I can tell...

由于构造函数参数在 Person 中没有 val/var 声明,并且由于 Person 不是 case 类,因此参数将不是类 Person 的成员,而只是构造函数参数。编译器本质上是在告诉你:嘿,你说的 firstName 和 lastName 是成员,它们覆盖/重新定义从基类继承的东西 - 但据我所知,没有什么......

class Person(val firstName: String, val lastName: String)
class Employee(fn: String, ln: String, val salary: BigDecimal) extends Person(fn, ln)

You do not need to declare firstName/lastName as overrides here, btw. Simply forwarding the values to the base class' constructor will do the trick.

顺便说一句,您不需要在这里将 firstName/lastName 声明为覆盖。只需将值转发到基类的构造函数即可。

回答by Sam Stainsby

You might also consider redesigning your super classes as traits as much as possible. Example:

您也可以考虑将您的超类重新设计为尽可能多的特征。例子:

trait Person {
  def firstName: String
  def lastName: String
}

class Employee(
  val firstName: String,
  val lastName: String,
  val department: String
) extends Person

or even

甚至

trait Employee extends Person {
  def department: String
}

class SimpleEmployee(
  val firstName: String,
  val lastName: String,
  val department: String
) extends Employee

回答by Synesso

Unless I've misunderstood your intention, here's how to extend Person.

除非我误解了您的意图,否则这里是如何扩展Person.

Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class Person( firstName: String, lastName: String)
defined class Person

scala> class Employee(firstName: String, lastName: String, depart: String) extends Person(firstName, lastName)
defined class Employee