日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scala 字段覆盖_Scala中的字段覆盖

發布時間:2025/3/11 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 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中的字段覆盖的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。