kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例
kotlin 覆盖属性
方法重载 (Method Overriding)
Method overriding allows derived class has the same function name and signature as the base class
方法重写允许派生类具有与基类相同的函数名称和签名
By method overriding we can provide different implementation into the derived class of base class methods.
通过方法重写,我们可以为基类方法的派生类提供不同的实现。
By default method is final in Kotlin class, to make them overridable declare the method with 'open'
默认情况下,方法在Kotlin类中是final,要使其可重写,请使用“ open”声明该方法
The open modifier does not affect when added on members of a final class (i.e.. a class with no open modifier).
当将open修饰符添加到最终类的成员(即没有open修饰符的类)的成员上时,则不受影响。
Marked function with 'override' into derived class while overriding the base class method.
在覆盖基类方法的同时,将具有“覆盖”功能的标记为派生类。
Use 'super' to call the base class implementation of the method from child class
使用“ super”从子类中调用方法的基类实现
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 覆盖属性
总结
以上是生活随笔为你收集整理的kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 狮子多少钱一只啊?
- 下一篇: 斯威夫特山地车_斯威夫特| 两个数字相加