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中的字段,必须牢记一些规则,以避免在实施此概念时出现错误,

  1. 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关键字 。 如果您错过了关键字,将提示错误并停止执行代码。

  2. Only fields that are defined using the val keyboard for defining fields in both parent class and child class can be overridden.

    只能覆盖使用val键盘定义的用于定义父类和子类中字段的字段。

  3. 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' modifierval voice = "Dog bark's...!"^
error: overriding value voice in class animal of type String;value voice needs `override' modifierval 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 variableoverride var voice = "Dog bark's...!"^
error: overriding variable voice in class animal of type String;variable voice cannot override a mutable variableoverride var voice = "Cat meow's...!";^
two errors found

翻译自: https://www.includehelp.com/scala/field-overriding-in-scala.aspx

scala 字段覆盖

scala 字段覆盖_Scala中的字段覆盖相关推荐

  1. cp linux 直接覆盖,Linux中cp直接覆盖不提示的方法

    新做了服务器,cp覆盖时,无论加什么参数-f之类的还是提示是否覆盖,这在大量cp覆盖操作的时候是不能忍受的. 把a目录下的文件复制到b目录 cp –r a/* b 执行上面的命令时,b存在的每个文件都 ...

  2. java jpa 字段 关联_jpaQuery中查询字段是关联表的查询方法以及@JoinEntity(joinEntityAlias =str)的作用和代码编写的规范...

    实体类的参数 @JoinEntity(joinEntityAlias = T_DeviceMoxing_AlarmRule,...)的作用 课室类 @Entity public class Class ...

  3. java 不同包子类 覆盖_Java中不同方法的覆盖方法

    经过测试 覆盖的好处是能够定义特定于子类类型的行为,这意味着子类可以根据其要求实现父类方法. 用面向对象的术语来说,覆盖是指覆盖现有方法的功能. 示例class Animal { public voi ...

  4. scala 方法重载_Scala中的方法重载

    scala 方法重载 Scala方法重载 (Scala method overloading) Method overloading is a method that is redefined in ...

  5. scala元组 数组_Scala中的数组

    scala元组 数组 Scala中的数组 (Arrays in Scala) An array is a linear data structure with a fixed number of el ...

  6. scala 方法调用_Scala中的方法调用

    scala 方法调用 Scala方法调用 (Scala Method Invocation) Method invocation is the legal and correct technique ...

  7. 获取字段_数据库中敏感字段的标记、标示

    数据治理需要面临和解决的最重要的问题是,企业这么多的数据库,每个数据库这么多的表,每个表这么多的字段,如何进行信息资产的分类分级. 通过以下方法,可以自动的取企业所有数据库.所有表.所有字段,根据字段 ...

  8. java 静态方法覆盖_Java中方法的覆盖和静态方法的隐藏

    下面的程序对巴辛吉小鬣狗和其它狗之间的行为差异进行了建模.如果你不知道 什么是巴辛吉小鬣狗,那么我告诉你,这是一种产自非洲的小型卷尾狗,它们从 来都不叫唤.那么,这个程序将打印出什么呢? class ...

  9. scala 字符串函数_Scala中的字符串chomp(或chop)函数

    scala 字符串函数 剁或剁弦 (Chop or Chomp string) It is used to chop off the end of line characters. For this ...

最新文章

  1. 云炬Qtpy5开发与实战笔记 3PyCharm添加PyUIC扩展——将.ui文件转换成.py文件
  2. 网页设计作业_Dreamweaver简单网页成品
  3. GDCM:排序图片的测试程序
  4. QML基础类型之real
  5. mysql 时间 本周 本月_mysql查询当天、本周、上周、本月、上月信息
  6. python中数据用折线图表示_用python处理文本数据
  7. [JavaWeb-JavaScript]JavaScript特殊语法
  8. (王道408考研操作系统)第二章进程管理-第一节5:线程概念和多线程模型
  9. 很遗憾AI还不能审查出儿童性侵录像:沙漠也被当成小黄片
  10. win7+VMware+Ubuntu16.04
  11. Python 代码实现验证码识别
  12. 黑客学习路线(送给那些在学习路上迷茫的人)
  13. 互联网中---外包的含义
  14. 鹤舞云天服务器稳定,《御剑红尘》手游新服“鹤舞云天”即将开启!
  15. mysql学习-Innodb行格式compact行记录解析
  16. 天池-金融风控训练营-task2-数据分析
  17. neo4j启动命令的小tips
  18. 聊聊关于复杂调查加权中权重对数据的分布影响
  19. ExpressBox 7 – RAS
  20. 方阵可逆,方阵行列式≠0,方阵满秩三者关系推导

热门文章

  1. 【Java从入门到头秃专栏 】(一)学在Java语法之前
  2. Redis(十二):Redis事务的基本操作
  3. UVA1025——A Spy in the Metro【dp】
  4. 转:6.1海量数据处理
  5. 不用正则表达式,用javascript从零写一个模板引擎(一)
  6. python(33)多进程和多线程的区别
  7. js的client、scroll、offset详解与兼容性
  8. 固有属性与自定义属性
  9. 关于 java 实现 语音朗读
  10. 堆和栈、值类型与引用类型、装箱与拆箱