kotlin半生對象_Kotlin程序| 隨播對象特征

kotlin半生對象

伴侶對象 (Companion object)

  • If you need a function or a property to be tied to a class rather than to instances of it (similar to static in java), you can declare it inside a companion object:

    如果需要將函數或屬性綁定到類而不是實例(類似于java中的static),則可以在同伴對象中聲明它:

  • You can omit the name, in which case the name defaults to Companion,

    您可以省略名稱,在這種情況下,名稱默認為Companion,

    companion object <Optional Name>{
    //Companion Object Body
    }
    
    
  • Companion objects members can only be accessed via the containing class name, not via instances of the class.

    伴侶對象成員只能通過包含的類名稱訪問,而不能通過類的實例訪問。

  • A class has only one companion object.

    一個類只有一個伴隨對象。

  • Companion object initializes when class is loaded, (typically when first time reference from other code).

    伴侶對象在加載類時初始化(通常是在第一次從其他代碼引用時)。

  • Companion object has its own init block.

    伴隨對象具有其自己的init塊。

  • The companion object is a singleton.

    伴隨對象是單例。

Kotlin中的伴隨對象特征程序 (Program for companion object features in Kotlin)

package com.includehelp
//Declare class
class Car{
//class init block
init {
println("Init Block of Class")
}
//Make companion object
companion object {
//companion object init block
init {
println("Init Block of Companion object")
}
//property of companion object
val name="Tata Altroz !! "
//function in companion object
fun printName(){
println("Your Car name : $name")
}
}
}
//Main Function, Entry Point of Program
fun main(){
//Call method with Class name, 
//without create Instance of class, 
//like static method in java
Car.printName()
//access Property using class name
val nameLen = Car.name.length
println("Car Name Length : $nameLen")
}

Output:

輸出:

Init Block of Companion object
Your Car name : Tata Altroz !! 
Car Name Length : 15

翻譯自: https://www.includehelp.com/kotlin/companion-object-features.aspx

kotlin半生對象

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

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

相關文章

mysql安裝注意步驟,mysql安裝步驟

mysql安裝步驟1、在官網下載對應的壓縮文件&#xff0c;放到本地文件夾下&#xff0c;解壓縮。2、配置Path環境變量&#xff1a;新增mysql的bin文件夾路徑&#xff0c;C:\software\mysql-8.0.23-winx64\bin。3、在mysql根目錄下新增my.ini配置文件。內容如下&#xff0c;basedi…

maven插件介紹之tomcat7-maven-plugin

tomcat7-maven-plugin插件的pom.xml依賴為&#xff1a; <dependency><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version> </dependency>一&#xff1a;直接執行…

在Python中模擬do-while循環

Python as a language doesnt support the do-while loop. However, we can have a workaround to emulate the do-while loop. Python作為一種語言不支持do-while循環。 但是&#xff0c;我們可以采用一種變通方法來模擬do-while循環 。 The syntax for do-while is as follo…

織夢cms生成首頁html的php文件,織夢DedeCMS定時自動生成首頁HTML的實現方法

只需要制作一個文件然后在首頁模板添加一句代碼就可以實現讓織夢DedeCMS自動生成首頁html&#xff0c;具體方法如下&#xff1a;第一步、需要在首頁調用隨機文章&#xff0c;這樣每次自動更新才會有更新的效果&#xff0c;隨機文章調用標簽如下&#xff1a;{dede:arclist sortr…

Linux下安裝Flume

1 下載Flume Welcome to Apache Flume — Apache Flume 下載1.9.0版本 2 上傳服務器并解壓安裝 3 刪除lib目錄下的guava-11.0.2.jar &#xff08;如同服務器安裝了hadoop&#xff0c;則刪除&#xff0c;如沒有安裝hadoop則保留這個文件&#xff0c;否則無法啟動flume&#…

Apple新發布的APFS文件系統對用戶意味著什么

2016年WWDC大會上&#xff0c;Apple除了公布watchOS、tvOS、macOS以及iOS等一系列系統和軟件更新外&#xff0c;還公布了一個名為APFS&#xff08;Apple File System&#xff09;的文件系統。 這一全新文件系統專門針對閃存/SSD進行優化&#xff08;但依然可用于傳統機械硬盤&a…

chown –r mysql:mysql,mysql部署,操作及異常處理

1、將mysql-5.1.50-linux-x86_64-glibc23.tar.gz移至/usr/local/目錄下&#xff0c;并改名為mysql增加mysql組#groupadd mysql建mysql用戶&#xff0c;并加入到mysql組中#useradd –g mysql mysql源碼包解壓#tar mysql-5.1.50-linux-x86_64-glibc23.tar.gz將解壓后的源碼包放置…

光伏等新能源信用風險事件頻繁爆發

2016年以來&#xff0c;伴隨著供給側改革相關政策陸續出臺和落地&#xff0c;去產能、去杠桿誘發信用風險事件陸續爆出。而在“11天威NTN1”、“15云峰PPN001”及“15云峰PPN003”等信用風險事件上&#xff0c;大股東“棄車保帥”行為再現&#xff0c;令本就失去造血能力的企業…

ruby array_Ruby中帶有示例的Array.zip()方法

ruby arrayArray.zip()方法 (Array.zip() Method) In this article, we will study about Array.zip() Method. You all must be thinking the method must be doing something which is related to zipping values of the Array instance. It is not as simple as it looks. W…

matlab中迪杰斯特拉算法,dijkstra算法(迪杰斯特拉算法)

單源最短路徑算法——Dijkstra算法&lpar;迪杰斯特拉算法&rpar;一 綜述 Dijkstra算法(迪杰斯特拉算法)主要是用于求解有向圖中單源最短路徑問題.其本質是基于貪心策略的(具體見下文).其基本原理如下: (1)初始化:集合vertex_set初始為{sourc ...Dijkstra【迪杰斯特拉算法…

關于概率算法的問題,不知道邏輯錯在哪里,求debug

做個骰子成功幾率的分析&#xff0c;投n顆骰子&#xff0c;第一次投成功的幾率是a,然后投成功的骰子&#xff0c;需要再投1次&#xff0c;這次成功的幾率是b。第二次成功的骰子才算最終成功。 要分析出n顆骰子&#xff0c;最終成功0到n顆的概率。 我寫了個算法&#xff0c;求出…

tps 交易量_交易處理系統(TPS)

tps 交易量A transaction is a simple process that takes place during business operations. The transaction processing system (TPS) manages the business transactions of the client and therefore helps a companys operations. A TPS registers, as well as all of i…

matlab for循環不覆蓋,將輸出保存到文本文件而不覆蓋和打印矩陣中的N個條目[matlab]...

這是代碼&#xff1a;for i 1:4;fileID fopen(testdata.txt, at);fprintf(fileID, this is answer %d\n,i);fprintf(fileID, %5.3e\n, v{i});fclose(fileID);end在記事本中回答&#xff1a;this is answer 11.000e0001.000e0001.000e0001.000e0001.000e0001.000e0000.000e0001…

(轉)Redis研究(一)—簡介

http://blog.csdn.net/wtyvhreal/article/details/41855327 Redis是一個開源的高性能鍵值對數據庫。它通過提供多種鍵值數據類型來適應不同場景下的存儲需求&#xff0c;并借助許多高層級的接口使其可以勝任如緩存、隊列系統等不同的角色。 1.1歷史和發展 2008年&#xff0c;意…

c bitset get_Java BitSet get()方法與示例

c bitset getBitSet類的get()方法 (BitSet Class get() method) Syntax: 句法&#xff1a; public boolean get(int bit_in);public BitSet get(int st_in, int en_in);get() method is available in java.util package. get()方法在java.util包中可用。 get(int bit_in) meth…

有擾動的閉環傳遞函數 matlab,(d)閉環系統的誤差傳遞函數.PPT

(d)閉環系統的誤差傳遞函數3. 控制系統的方框圖模型 若已知控制系統的方框圖,使用MATLAB函數可實現方框圖轉換。 a).串聯 如圖所示G1(s)和G2(s)相串聯,在MATLAB中可用串聯函數series( )來求G1(s)G2(s),其調用格式為 [num,den]series(num1,den1,num2,den2) 其中&#xff1a; b)并…

CYQ.Data 輕量數據層之路 自定義MDataTable綁定續章(七)

本章起&#xff0c;將續章講解整框架當初的設計思路&#xff1a; 本章既為續章&#xff0c;說明我以前寫過&#xff0c;是的&#xff0c;以前我寫過內部整個MDataTable的構造&#xff0c;不過&#xff0c;當初匆匆寫完后&#xff0c; 最后一步的實現MDataTable綁定GridView/Dat…

php 文字超出畫布,input實現文字超出省略號(代碼示例)

本篇文章給大家帶來的內容是關于input實現文字超出省略號(代碼示例)&#xff0c;有一定的參考價值&#xff0c;有需要的朋友可以參考一下&#xff0c;希望對你有所幫助。input實現文字省略號功能普通元素實現文字超出寬度自動變成省略號非常簡單&#xff0c;給元素加個寬度&…

c++ stl stack_C ++ STL中的stack :: top()函數

c stl stackPrototype: 原型&#xff1a; stack<T> st; //declarationT st.top();Parameter: 參數&#xff1a; No parameter passedReturn type: T //data type 返回類型&#xff1a; T //數據類型 Header file to be included: 包含的頭文件&#xff1a; #include …

排序算法系列:插入排序算法

概述 直接插入排序&#xff08;Straight Insertion Sort&#xff09;的基本操作是將一個記錄插入到已經排好序的有序表中&#xff0c;從而得到一個新的、記錄數增1的有序表。 – 《大話數據結構》 版權說明 著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載…