日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

kotlin实现继承_Kotlin程序| 继承的例子

發(fā)布時(shí)間:2023/12/1 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kotlin实现继承_Kotlin程序| 继承的例子 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

kotlin實(shí)現(xiàn)繼承

遺產(chǎn) (Inheritance)

  • Inheritance is a mechanism wherein a new class is derived from an existing class.

    繼承是一種機(jī)制,其中新類是從現(xiàn)有類派生的。

  • All Kotlin Classes have a common Superclass 'Any', it is the Default Superclass with no Super Type declared.

    所有Kotlin類都有一個(gè)通用的超類“ Any”,它是沒(méi)有聲明任何超類型的默認(rèn)超類。

    class Student // implicitly inherits from Any
  • 'Any' Class has three methods

    “任何”課程有三種方法

  • equals()
  • hashCode()
  • toString()
  • By Default All class is Final in Kotlin, They can't be inherited.

    默認(rèn)情況下,所有類在Kotlin中都是Final,不能繼承。

  • Use 'open' keyword, to make class inheritable,

    使用“ open”關(guān)鍵字,使類可繼承,

    open class <class name> // make class inheritable
  • To declare explicit supertype, placer base class name after colon into the class header.

    要聲明顯式超類,在冒號(hào)之后將放置器基類名稱放入類標(biāo)題中。

    open class Base{} class Derived : Base{}

Kotlin繼承計(jì)劃 (Program for Inheritance in Kotlin)

package com.includehelp//Declare class, use 'open' keyword //to make class inheritable //Class without declare primary constructor open class Company{//member functionfun getCompany(){println("Base Methods : Company Info")} } //Declare class using inheritance class Department : Company() {//member functionfun getDept(){println("Derived Method : Dept Info")} }//Declare class, use 'open' keyword //to make class inheritable open class Shape(val a:Int){fun getBaseValue(){println("Base value : $a")} } //if derived class has primary constructor //then base class can initialized there //using primary constructor parameters class Circle(val b:Int): Shape(b) {fun getChildValue(){println("Child value : $b")} }//Main Function, Entry point of program fun main(args:Array<String>){//create derived class objectval company = Department()//access base class functioncompany.getCompany()//access derived class functioncompany.getDept()//create derived class object and passed parameterval shape = Circle(20)//access base class functionshape.getBaseValue()//access derived class functionshape.getChildValue() }

Output:

輸出:

Base Methods : Company Info Derived Method : Dept Info Base value : 20 Child value : 20

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

kotlin實(shí)現(xiàn)繼承

總結(jié)

以上是生活随笔為你收集整理的kotlin实现继承_Kotlin程序| 继承的例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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