博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例
阅读量:2528 次
发布时间:2019-05-11

本文共 2322 字,大约阅读时间需要 7 分钟。

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 inheritableopen class Person{    //marked function with 'open' to make    //overridable    open fun printMessage(){        println("Message for Person")    }}//Derived class extends Person classclass Child: Person() {    //Override base class methods    override fun printMessage(){        println("Message for Child")    }}//marked derived class with 'open' //to make further inheritable by its //child classopen 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 final        final override fun printMessage(){            println("Message for Boys")        }}//Derived classclass Hero : Boy() {    //Declare member function    fun printData(){        //calling , Boy Class Implementation         //of printMessage() function        super.printMessage()        println("Hello Hero")    }}fun main(args:Array
){ //Create Person class Instance and Called Methods, //it will invoke Base class version of methods Person().printMessage() //Create class Instance and Called Methods, //it will invoke Child class version of methods Child().printMessage() //Create class Instance and Called Methods Hero().printData()}

Output:

输出:

Message for PersonMessage for ChildMessage for BoysHello Hero

翻译自:

kotlin 覆盖属性

转载地址:http://zhxzd.baihongyu.com/

你可能感兴趣的文章
uni-app 引入本地iconfont的正确姿势以及阿里图标引入
查看>>
DSB
查看>>
Java中的阻塞队列
查看>>
前端软件sublime的一些常用快捷键
查看>>
openssl 升级
查看>>
2017.10.30 天晴 昨天十公里没减肥
查看>>
Git 打标签(分布式版本控制系统)
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
HttpClient读取数据乱码的解决方案
查看>>
如何使用FireBug插件查询元素的xPath属性
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>