kotlin實現繼承_Kotlin程序| 繼承的例子

kotlin實現繼承

遺產 (Inheritance)

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

    繼承是一種機制,其中新類是從現有類派生的。

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

    所有Kotlin類都有一個通用的超類“ Any”,它是沒有聲明任何超類型的默認超類。

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

    “任何”課程有三種方法

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

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

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

    使用“ open”關鍵字,使類可繼承,

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

    要聲明顯式超類,在冒號之后將放置器基類名稱放入類標題中。

    open class Base{}
    class Derived : Base{}
    
    

Kotlin繼承計劃 (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 function
fun getCompany(){
println("Base Methods : Company Info")
}
}
//Declare class using inheritance
class Department : Company() {
//member function
fun 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 object
val company = Department()
//access base class function
company.getCompany()
//access derived class function
company.getDept()
//create derived class object and passed parameter
val shape = Circle(20)
//access base class function
shape.getBaseValue()
//access derived class function
shape.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實現繼承

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377697.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377697.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377697.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【C++grammar】動態類型轉換、typeid與RTTI

目錄動態類型轉換1、為何需要動態類型轉換2、dynamic_cast<>();運算符3、向上轉換和向下轉換( Upcasting and Downcasting)4、 基類對象和派生類對象的互操作5、Upcasting/Downcasting與繼承鏈上不同類的對象之間的賦值有什么關系和區別&#xff1f;typeid 運行時查詢類型…

nginx資源定向 css js路徑問題

今天玩玩項目&#xff0c;學學nginx發現還不錯&#xff0c;速度還可以&#xff0c;但是CSS JS確無法使用&#xff0c;原來Iginx配置時需要對不同類型的文件配置規則&#xff0c;真是很郁悶&#xff0c;不過想想也還是很有道理。閑暇之際&#xff0c;把配置貼上來。#user nobody…

五、庫存查詢功能的完善

一、數據庫的建立 由于查詢功能和之前的 入庫管理功能 所用的數據庫都一樣&#xff0c;這里仍使用yy_textile表 在fiber_yy數據庫下創建yy_textile表 初始數據庫信息 二、頁面的完善 登錄注冊頁面我就不演示了&#xff0c;前幾篇博文也都有介紹 query查詢頁面 main_page…

整合ajaxmin 和 less 到VS.net

我用的前端框架是bootstrap_extra, twitter團隊做的&#xff0c;這個是他的一個擴展&#xff0c;首先從上面下載一個。至于ajaxmin&#xff0c;請參考這里1) 從bootstrap_extra的解壓包中&#xff0c;復制build目錄下三個文件到項目中去&#xff0c;這三個文件分別是BatchSubsi…

轉:只能選擇GridView中的一個CheckBox(單選CheckBox)

方法1&#xff1a; protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e){CheckBox cbx e.Row.FindControl("cbID") as CheckBox;try{//綁定選中CheckBox 客戶端IDcbx.Attributes.Add("onclick", "Change(" cbx.Cli…

六、出庫管理功能的實現

一、數據庫的建立 這里仍使用yy_textile表 在fiber_yy數據庫下創建yy_textile表 初始數據庫信息 二、頁面的完善 登錄注冊頁面我就不演示了&#xff0c;前幾篇博文也都有介紹 shipment出庫管理頁面 main_page頁面進行功能完善 三、代碼實現 shipment出庫管理頁面 u…

數學建模:層次分析法實例以及代碼

博主聯系方式&#xff1a; QQ:1540984562 QQ交流群&#xff1a;892023501 群里會有往屆的smarters和電賽選手&#xff0c;群里也會不時分享一些有用的資料&#xff0c;有問題可以在群里多問問。 目錄層次分析法的思想層次分析法步驟具體案例(市政工程項目建設決策)1.問題提出2.…

c 僵尸進程_演示僵尸進程的C程序

c 僵尸進程僵尸進程 (Zombie process) A process which has finished its execution but still has an entry in the process table to report to its parent process is known as a zombie process. 一個已經完成執行但仍在進程表中具有要報告給其父進程的條目的進程稱為僵尸進…

探秘IntelliJ IDEA 13測試版新功能——調試器顯示本地變量

IntelliJ IDEA在業界被公認為最好的Java開發平臺之一&#xff0c;JetBrains公司將在12月正式發布IntelliJ IDEA 13版本。 現在&#xff0c;小編將和大家一起探秘密IntelliJ IDEA 13測試版本新功能——調試器顯示本地變量。這個功能非常強大&#xff0c;調試器可以顯示變量&…

C# Windows Form下的控件的Validator(數據驗證)

由于偶爾的一個想法&#xff0c;謀生了一個做一個windows form下的Validator控件&#xff0c;或者直接說類吧&#xff01; 因為webform下的Validator控件太好用了。哈哈&#xff0c;直接看代碼&#xff01; 下面這個類&#xff0c;主要是一個簡單的驗證類&#xff0c;不過只是起…

七、流水查詢---記錄用戶登錄信息

一、數據庫的建立 在fiber_yy數據庫下創建yy_user_record表 可以先手動填入幾條數據信息 初始數據庫信息 username為用戶賬號 sex為用戶注冊所填寫的性別 phone為用戶手機號 time為用戶登錄該系統的時間 二、頁面的設計 登錄注冊頁面我就不演示了&#xff0c;前幾篇博文…

leetcode 455. 分發餅干 思考分析

目錄題目自己的思路以及AC代碼參考思路題目 假設你是一位很棒的家長&#xff0c;想要給你的孩子們一些小餅干。但是&#xff0c;每個孩子最多只能給一塊餅干。 對每個孩子 i&#xff0c;都有一個胃口值 g[i]&#xff0c;這是能讓孩子們滿足胃口的餅干的最小尺寸&#xff1b;并…

c++ cdi+示例_C ++'not'關鍵字和示例

c cdi示例"not" is an inbuilt keyword that has been around since at least C98. It is an alternative to ! (Logical NOT) operator and it mostly uses with the conditions. “ not”是一個內置關鍵字&#xff0c;至少從C 98起就存在。 它是替代&#xff01; …

【second】Flatten Binary Tree to Linked List

遞歸 void flatten(TreeNode *root) {// Note: The Solution object is instantiated only once and is reused by each test case.flat(root);}TreeNode* flat(TreeNode* root){if(!root)return NULL;TreeNode* left_tail flat(root->left);TreeNode* right_tail flat(ro…

八、流水查詢---記錄紡織品出庫信息

一、數據庫的建立 在fiber_yy數據庫下創建yy_textile_record表 可以先手動填入幾條數據信息 初始數據庫信息 第一條數據的username是空格不是null number為織物的品號(唯一的) stock為出貨量 username為哪個賬號 time為出貨時間 二、頁面的完善 登錄注冊頁面我就不演示…

應用程序欄【WP7學習札記之九】

本節是WP7學習札記的第九篇&#xff0c;講的是系統托盤和應用程序欄&#xff0c;具體內容是系統托盤和應用程序欄的介紹&#xff0c;如何分別使用C#、xaml以及Expression Blend生成應用程序欄&#xff0c;應用程序欄的透明度以及對屏幕方向改變的支持。摘要如下&#xff1a; 系…

橢圓曲線密碼學導論pdf_橢圓曲線密碼學

橢圓曲線密碼學導論pdf歷史 (History) The use of elliptic curves in cryptography was advised independently by Neal Koblitz and Victor S. Miller in 1985. Elliptic curve cryptography algorithms entered large use from 2004 to 2005. 1985年&#xff0c; Neal Kobli…

leetcode 第 216 場周賽 整理

目錄1662. 檢查兩個字符串數組是否相等題目自己代碼5606. 具有給定數值的最小字符串題目自己代碼貪心算法1664. 生成平衡數組的方案數題目自己代碼動態規劃優化1665. 完成所有任務的最少初始能量題目思路1662. 檢查兩個字符串數組是否相等 題目 給你兩個字符串數組 word1 和 …

九、忘記密碼功能的實現

一、頁面設計 login頁面&#xff0c;和第二篇博文(用戶登錄和注冊)頁面基本一樣&#xff0c;只不過多了一個按鈕 其中忘記密碼&#xff1f;點我找回 為button3 retrieve_password頁面 change_password頁面 頁面如下&#xff1a; 二、數據庫 因為是忘記密碼&#xff0c;…

Android中對手機文件進行讀寫

參考張澤華視頻 &#xff08;一&#xff09;讀寫手機內存卡中的文件 對手機中的文件進行讀寫操作&#xff0c;或者新增一個文件時&#xff0c;可直接使用openFileOutput / openFileInput 得到文件的輸出、輸入流。 FileOutputStream fos this.openFileOutput("private.…