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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

kotlin获取属性_Kotlin程序| 属性获取器和设置器方法的示例

發(fā)布時間:2025/3/11 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kotlin获取属性_Kotlin程序| 属性获取器和设置器方法的示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

kotlin獲取屬性

屬性獲取器和設置器方法 (Properties Getter and Setter Methods)

  • Variable having a class-level scope, declared inside the class body but outside the functions called property.

    具有類級別范圍的變量,在類主體內部但在稱為屬性的函數(shù)外部聲明。

  • Property can be declared with var(mutable) and val (read-only).

    可以使用var(mutable)和val(只讀)聲明屬性。

    var/val <propertyName>: <PropertyType> = <property_initializer> [<getter>] [<setter>]
  • property_initializer, getter, and Setter are optional.

    property_initializer,getter和Setter是可選的。

  • Getter and Setter Auto-Generated into the code.

    Getter和Setter自動生成到代碼中。

  • Getter is used to get the value of properties and setter is used to set value of properties.

    Getter用于獲取屬性值,而setter用于設置屬性值。

  • val(read-only) type property does not allow setter.

    val(只讀)類型屬性不允許使用setter。

  • If we don't want public access of setter than declare it private.

    如果我們不希望公開訪問setter,則將其聲明為私有。

    var name:String private set

程序以演示Kotlin中的屬性Getter和Setter方法的示例 (Program to demonstrate the example of Properties Getter and Setter Methods in Kotlin)

package com.includehelp// Declare class, class America{// Declare property with initial valuevar city:String = "NewYork"// Auto Generated getter and setter }// Declare class, class India{// Declare property with initial valuevar city:String = "Delhi"// define optional getter and setterget() = field // Getterset(value) { // Setterfield=value} }// Declare class, define optional getter and setter class China{// Declare property with initial valuevar city:String = "Wuhan"// private setter, cant set value from outside the classprivate set// member function to set propertyfun setCity(city:String){this.city=city}}// declare class, with customized getter and setter class Japan{// Declare property with initial valuevar city:String = "Tokyo"// Getter of propertyget() = field.toUpperCase()//setter of Propertyset(value) {field="Modern City $value"} }// Main function, entry Point of Program fun main(){// create Instanceval america=America()america.city="Alsakaaa" // access setterprintln("America : ${america.city}") // access getter// create Instanceval india=India()india.city="Mumbai" // access setterprintln("India : ${india.city}") // access getter// create Instanceval china=China()// Try to access private setter, leads to compile time error// china.city="Beijing"// Set City by calling member functionchina.setCity("Beijing")println("China : ${china.city}") // access getter// create Instanceval japan=Japan()india.city="Quoto" // access setterprintln("Japan : ${india.city}") // access getter }

Output:

輸出:

America : Alsakaaa India : Mumbai China : Beijing Japan : Quoto

翻譯自: https://www.includehelp.com/kotlin/example-of-properties-getter-and-setter-methods.aspx

kotlin獲取屬性

總結

以上是生活随笔為你收集整理的kotlin获取属性_Kotlin程序| 属性获取器和设置器方法的示例的全部內容,希望文章能夠幫你解決所遇到的問題。

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