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获取属性

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

  1. 关闭惠普计算机通电启动注册表,惠普10代cpu电脑装win7卡logo(安装程序正在更新注册表设置)解决方法...

    最近有很多网友问我惠普电脑10代cpu台式机安装win7卡在在更新注册表设置怎么办?出现这种情况一般情况是由于没有提前注入显卡驱动,现在很多新电脑都采用的集成显卡,10代以cpu采用的集显的话,在ue ...

  2. kotlin获取属性_Kotlin程序获取系统MAC地址

    kotlin获取属性 The task is to get system MAC address. 任务是获取系统MAC地址. package com.includehelp import java. ...

  3. kotlin获取属性_Kotlin程序获取系统名称

    kotlin获取属性 The task is to get the system name. 任务是获取系统名称. package com.includehelp import java.net.In ...

  4. kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例

    kotlin 覆盖属性 方法重载 (Method Overriding) Method overriding allows derived class has the same function na ...

  5. kotlin实现继承_Kotlin程序| 继承的例子

    kotlin实现继承 遗产 (Inheritance) Inheritance is a mechanism wherein a new class is derived from an existi ...

  6. kotlin 计算平方_Kotlin程序来计算复利

    kotlin 计算平方 Compound interest is the sum of principal amount and interest of interest. 复利是本金和利息之和. G ...

  7. kotlin 计算平方_Kotlin程序计算自然数之和

    kotlin 计算平方 Given a number number, and we have to calculate the sum of all natural numbers from 1 to ...

  8. kotlin 或 运算_Kotlin程序对两个数字执行算术运算

    kotlin 或 运算 Here, we are implementing a Kotlin program to perform various arithmetic operations on t ...

  9. kotlin 判断数字_Kotlin程序可以逆转数字

    kotlin 判断数字 Given an integer number, we have to find reverse number and print it. 给定一个整数,我们必须找到反向数字并 ...

最新文章

  1. 秒杀(PHP,Redis,Lua)
  2. android 设置键盘弹出动画,Android实现键盘弹出界面上移的实现思路
  3. Android Binder机制(1501210451 张志康)
  4. Relaxation step(Dijkstra's 最短路径算法)
  5. 旋风加速安卓Android官网,旋风加速官网版最新版
  6. 有哪些令人骄傲的国产软件或应用?这5款你一定不知道!
  7. win10QQ语音无法使用麦克风
  8. 耗纸古文风之四--登高
  9. Timed out waiting for process (xxx) to appear on错误
  10. python请输入一个三位数输出该三位数的逆序数_编写程序,从键盘输入一个三位数,求出其逆序数并输出,例如输入123,输出321。...
  11. CSDN博客之星评选
  12. 全网最详细的Python+Requests接口测试教程:Fiddler抓包工具
  13. Arnold阿诺德渲染器:C4DtoA for Cinema4D R20 for Mac
  14. Lenovo T460 Fn功能键切换
  15. HTML5编写船上航行网页,前端面试整理(HTMLCSS) | 航行学园
  16. Broekett定理):
  17. 布林带-BOLL (Bollinger Band)
  18. 最新 UltraEdit 24.20 注册成功经验分享
  19. linux 防火墙设置ftp端口,CentOS 配置防火墙操作实例(启、停、开、闭端口)CentOS Linux-FTP/对外开放端口(接口)TomCat相关...
  20. JS中预处理是一种好无节操的机制

热门文章

  1. uint8 转换为 float
  2. BUAA 436 孟竹的复习计划(二维树状数组)
  3. NestedScrolling机制
  4. linuxSAMBA共享
  5. 粗略的看JFinal的基于AOP的拦截器的实现
  6. MySQL运维常用系统命令
  7. 马老师 生产环境mysql主从复制、架构优化方案
  8. 快速配置 Samba 将 Linux 目录映射为 Windows 驱动器
  9. Oracle分析函数一——函数列表
  10. [转]使用URLConnection下载文件或图片并保存到本地