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

kotlin获取属性

属性获取器和设置器方法 (Properties Getter and Setter Methods)

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

    具有类级别范围的变量,在类主体内部但在称为属性的函数外部声明。

  • 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 value
    var city:String = "NewYork"
    // Auto Generated getter and setter
}

// Declare class,
class India{
    // Declare property with initial value
    var city:String = "Delhi"

    // define optional getter and setter
    get() = field // Getter
    set(value) {  // Setter
        field=value
    }
}

// Declare class, define optional getter and setter
class China{
    // Declare property with initial value
    var city:String = "Wuhan"

    // private setter, cant set value from outside the class
    private set

    // member function to set property
    fun setCity(city:String){
        this.city=city
    }

}

// declare class, with customized getter and setter
class Japan{
    // Declare property with initial value
    var city:String = "Tokyo"

    // Getter of property
    get() = field.toUpperCase()

    //setter of Property
    set(value) {
        field="Modern City $value"
    }
}

// Main function, entry Point of Program
fun main(){
    // create Instance
    val america=America()
    america.city="Alsakaaa" // access setter
    println("America : ${america.city}") // access getter

    // create Instance
    val india=India()
    india.city="Mumbai" // access setter
    println("India : ${india.city}") // access getter

    // create Instance
    val china=China()
    // Try to access private setter, leads to compile time error
    // china.city="Beijing"

    // Set City by calling member function
    china.setCity("Beijing")
    println("China : ${china.city}") // access getter

    // create Instance
    val japan=Japan()
    india.city="Quoto" // access setter
    println("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获取属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值