日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kotlin半生对象_Kotlin程序| 随播对象特征

發布時間:2025/3/11 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 blockinit {println("Init Block of Class")}//Make companion objectcompanion object {//companion object init blockinit {println("Init Block of Companion object")}//property of companion objectval name="Tata Altroz !! "//function in companion objectfun 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 javaCar.printName()//access Property using class nameval nameLen = Car.name.lengthprintln("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半生對象

總結

以上是生活随笔為你收集整理的kotlin半生对象_Kotlin程序| 随播对象特征的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。