kotlin获取属性_Kotlin属性获取器和设置器

kotlin获取属性

定义自定义获取器 (Define a custom getter)

Kotlin has the convenience of custom computed properties in the form of custom getters. Here’s an example computed property from a real app I built which takes properties from a custom Location class and returns a short description of that location.

Kotlin以自定义获取器的形式提供了自定义计算属性的便利。 这是我构建的真实应用程序的示例计算属性,该应用程序从自定义Location类获取属性并返回该位置的简短说明。

定义自定义设置器 (Define a custom setter)

A custom setter will be called every time a property’s value is set. In my app I need to sometimes save a search result and its type to a Realm database, but I cannot save enum values — only primitive types. So in this case when the enum value of resultType is set, I also set a restultTypeString property for if/when I need to save it in Realm.

每次设置属性值时,都会调用一个自定义设置器。 在我的应用程序中,有时需要将搜索结果及其类型保存到Realm数据库中,但是我无法保存枚举值-仅保存原始类型。 因此,在这种情况下,当设置了resultType的枚举值时,我还将为if /当需要将其保存在Realm中时设置一个restultTypeString属性。

To learn more about Kotlin getters and setters, visit the official docs.

要了解有关Kotlin吸气器和吸气器的更多信息, 请访问官方文档

翻译自: https://medium.com/@rickycpadilla/kotlin-property-getters-and-setters-17ffb5a71b98

kotlin获取属性

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值