scala 字段覆盖
Scala字段覆盖 (Scala field overriding)
Overriding is the concept in which the child class is allowed to redefine the members of the parent class. Both methods and variables/ fields can be overridden in object-oriented programming. In Scala as well both methods and Fields can be overridden but overriding of fields has some restrictions on implementation.
覆盖是允许子类重新定义父类成员的概念。 在面向对象的编程中,方法和变量/字段都可以被覆盖。 在Scala中,方法和字段都可以被覆盖,但是字段的覆盖对实现有一些限制。
Scala中的覆盖字段 (Overriding fields in Scala)
For overriding fields in Scala, some rules must be kept in mind to avoid errors that arise while implementing this concept,
为了覆盖Scala中的字段,必须牢记一些规则,以避免在实施此概念时出现错误,
The use of override keyword is necessary to implement the concept field overriding. If you missed the keyword an error will be prompted and execution of the code will be stopped.
要实现概念字段重写,必须使用override关键字 。 如果您错过了关键字,将提示错误并停止执行代码。
Only fields that are defined using the val keyboard for defining fields in both parent class and child class can be overridden.
只能覆盖使用val键盘定义的用于定义父类和子类中字段的字段。
Fields that are defined using the var keyword cannot be overridden as they are editable ( that is both read and write operations are allowed on the variable) anywhere in the class.
使用var关键字定义的字段不能被覆盖,因为它们在类中的任何位置都是可编辑的(即,允许对变量进行读写操作)。
Here are some programs that show the implementation of field overriding and Scala.
这里有一些程序显示了字段覆盖和Scala的实现 。
1) Python program without override keyword
1)没有替代关键字的Python程序
This program will show what will be the output if override keyword is not used?
如果不使用override关键字,该程序将显示什么输出?
class animal{
val voice = "animal's voice"
}
class dog extends animal{
val voice = "Dog bark's...!"
def says(){
println(voice)
}
}
class cat extends animal{
val voice = "cat meow's...!";
def says(){
println(voice)
}
}
object MainObject{
def main(args:Array[String]){
var ani = new dog()
ani.says()
}
}
Output
输出量
error: overriding value voice in class animal of type String;
value voice needs `override' modifier
val voice = "Dog bark's...!"
^
error: overriding value voice in class animal of type String;
value voice needs `override' modifier
val voice = "cat meow's...!";
^
two errors found
2) Python program with override keyword
2)具有override关键字的Python程序
This program will show what will be the output if override keyword is used?
该程序将显示如果使用override关键字将输出什么?
class animal{
val voice = "animal's voice"
}
class dog extends animal{
override val voice = "Dog bark's...!"
def says(){
println(voice)
}
}
class cat extends animal{
override val voice = "Cat meow's...!";
def says(){
println(voice)
}
}
object MainObject{
def main(args:Array[String]){
var ani = new dog()
ani.says()
var ani2 = new cat()
ani2.says()
}
}
Output
输出量
Dog bark's...!
Cat meow's...!
3) Python program with var keyword
3)使用var关键字的Python程序
This program will show what will be the output if var keyword is used?
该程序将显示如果使用var关键字将输出什么?
class animal{
var voice = "animal's voice"
}
class dog extends animal{
override var voice = "Dog bark's...!"
def says(){
println(voice)
}
}
class cat extends animal{
override var voice = "Cat meow's...!";
def says(){
println(voice)
}
}
object MainObject{
def main(args:Array[String]){
var ani = new dog()
ani.says()
var ani2 = new cat()
ani2.says()
}
}
Output
输出量
error: overriding variable voice in class animal of type String;
variable voice cannot override a mutable variable
override var voice = "Dog bark's...!"
^
error: overriding variable voice in class animal of type String;
variable voice cannot override a mutable variable
override var voice = "Cat meow's...!";
^
two errors found
翻译自: https://www.includehelp.com/scala/field-overriding-in-scala.aspx
scala 字段覆盖