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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例

發(fā)布時(shí)間:2023/12/1 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

kotlin 覆蓋屬性

方法重載 (Method Overriding)

  • Method overriding allows derived class has the same function name and signature as the base class

    方法重寫允許派生類具有與基類相同的函數(shù)名稱和簽名

  • By method overriding we can provide different implementation into the derived class of base class methods.

    通過方法重寫,我們可以為基類方法的派生類提供不同的實(shí)現(xiàn)。

  • By default method is final in Kotlin class, to make them overridable declare the method with 'open'

    默認(rèn)情況下,方法在Kotlin類中是final,要使其可重寫,請(qǐng)使用“ open”聲明該方法

  • The open modifier does not affect when added on members of a final class (i.e.. a class with no open modifier).

    當(dāng)將open修飾符添加到最終類的成員(即沒有open修飾符的類)的成員上時(shí),則不受影響。

  • Marked function with 'override' into derived class while overriding the base class method.

    在覆蓋基類方法的同時(shí),將具有“覆蓋”功能的標(biāo)記為派生類。

  • Use 'super' to call the base class implementation of the method from child class

    使用“ super”從子類中調(diào)用方法的基類實(shí)現(xiàn)

Kotlin中的方法重寫程序 (Program for method overriding in Kotlin)

package com.includehelp//Declare Base class, //marked with 'open' to make inheritable open class Person{//marked function with 'open' to make//overridableopen fun printMessage(){println("Message for Person")} }//Derived class extends Person class class Child: Person() {//Override base class methodsoverride fun printMessage(){println("Message for Child")} }//marked derived class with 'open' //to make further inheritable by its //child class open class Boy : Person(){//A member marked override is itself open, //i.e. it may be overridden in subclasses.//If you want to prohibit re-overriding, use finalfinal override fun printMessage(){println("Message for Boys")} }//Derived class class Hero : Boy() {//Declare member functionfun printData(){//calling , Boy Class Implementation //of printMessage() functionsuper.printMessage()println("Hello Hero")} }fun main(args:Array<String>){//Create Person class Instance and Called Methods, //it will invoke Base class version of methodsPerson().printMessage()//Create class Instance and Called Methods,//it will invoke Child class version of methodsChild().printMessage()//Create class Instance and Called MethodsHero().printData() }

Output:

輸出:

Message for Person Message for Child Message for Boys Hello Hero

翻譯自: https://www.includehelp.com/kotlin/example-of-method-overriding.aspx

kotlin 覆蓋屬性

總結(jié)

以上是生活随笔為你收集整理的kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。