Code
  • 首页
  • cpu-architecture
  • performance
  • c++
  • branch-prediction
  • java
2025-01-04 05:00:06

Kotlin: Public get private set var

kotlin

在kotlin中定义具有公共getter和私有(仅可在内部修改)setter的var的正确方法是什么?


var setterVisibility: String = "abc" // Initializer required, not a nullable type
    private set // the setter is private and has the default implementation

参见:属性Getter和Setter

2015-10-30 06:47:38

你可以使用以下方法轻松做到这一点:

var atmosphericPressure: Double = 760.0
    get() = field
    private set(value) { 
        field = value 
    }

看看这个故事:Medium: Property, Getter和Setter in Kotlin。

2019-01-26 13:39:02

推荐文章

  • Kotlin中惯用的登录方式
  • 如何在Kotlin中实现生成器模式?
  • Kotlin中的单个感叹号
  • 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
  • 如何克隆或复制一个列表在kotlin
  • 如何将字符串转换为长在Kotlin?
  • 如何在Kotlin解析JSON ?
  • 什么Java 8流。收集等价物可在标准Kotlin库?
  • Android房间-简单的选择查询-不能访问数据库在主线程
  • 如何检查“instanceof”类在kotlin?
  • “by”关键字在Kotlin中做什么?
  • 导航目标xxx对于这个NavController是未知的
  • 我如何解决错误“minCompileSdk(31)指定在一个依赖的AAR元数据”在本机Java或Kotlin?
  • 模块是用不兼容的Kotlin版本编译的。其元数据的二进制版本为1.5.1,预期版本为1.1.15
  • Kotlin: Public get private set var

aliyun

最新文章

  • 如何删除默认的导航栏空间在SwiftUI导航视图
  • 我可以列出所有的标准Go包吗?
  • 证书验证失败:无法获得本地颁发者证书
  • 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
  • 打开同一目录两次
  • 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
  • NullInjectorError: AngularFirestore没有提供程序
  • 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
  • 从元组/数组值派生联合类型
  • 如何在Angular2中截断文本?
  • 安装tzdata非交互式
  • ionic 2 - Error在Android Studio中找不到Gradle的安装版本
  • 如何查看所有地区所有正在运行的Amazon EC2实例?
  • 当使用pip3安装包时,“Python中的ssl模块不可用”
  • 安装APK时出现错误

标签

cpu-architectureperformancec++branch-predictionjavaundogit-commitgitversion-controlgit-remotegit-pushgit-branchgit-pullgit-fetchpythongeneratoriteratorjsonmime-typescontent-typearraysjavascriptgit-addcode-formattingcstandards-complianceoperatorscommentsdynamic-memory-allocationstackmemory-managementheap-memorylanguage-agnosticoverwritebackground-colorbrowserhtmldomjqueryvisibilityuse-strictsyntaxjslintremote-branchgit-checkoutpython-moduleidiomsprogram-entry-pointnamespacesbranch
2025 code 京ICP备15047053号-1