scala 字段覆盖_Scala中的字段覆盖
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' 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 found2) 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中的字段覆盖的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scala 字符串函数_Scala中的字
- 下一篇: java system.load()_S