科特林。如何声明常量?

发布于 2025-01-15 18:54:16 字数 571 浏览 5 评论 0原文

我有一个类,它在主构造函数中有一些字段:

class SomeData(val counter: Int...) { // some logic}

我需要创建一个常量。我通常这样做:

 companion object {
     private const val MAX_VALUE = 1000
 }

但就我而言,要声明一个常量,我需要使用类 SomeData 中的字段。但是要从 SomeData 类访问字段 counter,我需要创建此类的实例,然后访问该字段。

做这样的事情是正常做法吗?

或者最好在类中声明这个常量:

private val MAX_VALUE = counter/ 2

但在这种情况下,Android Studio 会警告我:

私有属性名称“MAX_VALUE”不应包含下划线 中间或结尾

我应该如何声明一个常量?

I have class, which in primary constructor has some fields:

class SomeData(val counter: Int...) { // some logic}

I need to create a constant. I usually do it like this:

 companion object {
     private const val MAX_VALUE = 1000
 }

But in my case, to declare a constant, I need to use a field from the class SomeData. But to access the field counter from SomeData class I need to create an instance of this class and then access the field.

Is it normal practice to do something like this?

Or is it better to declare this constant inside the class:

private val MAX_VALUE = counter/ 2

But in that case, Android Studio warns me:

Private property name 'MAX_VALUE ' should not contain underscores in
the middle or the end

How should I declare a constant?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把人绕傻吧 2025-01-22 18:54:16

如果您的 MAX_VALUE 基于对象中的其他数据,那么根据定义,它不是常量。

相反,您需要的是只读属性。有两种简单的方法可以创建它:

首先,您已经做了什么:

class SomeData(val counter: Int...) {

    private val maxValue = counter / 2

    // some logic
}

请注意,名称 maxValue 使用驼峰式大小写,而不是大写蛇形大小写。

其次,如果您想更详细一点,您可以使用显式 getter:

class SomeData(val counter: Int...) {

    private val maxValue: String
        get() = counter / 2

    // some logic
}

第二种形式还意味着如果您的 counter 是一个可变的 var (而不是一个不可变的 val),那么当 counter 改变时,maxValue 也会改变。

If your MAX_VALUE is based on other data in the object, then it is by definition not a constant.

What you need instead is a read-only property. There are two easy ways to create this:

First, what you already did:

class SomeData(val counter: Int...) {

    private val maxValue = counter / 2

    // some logic
}

Note that the name maxValue is using camel-case, not upper-snake-case.

Second, if you want to be a little more verbose, you can use an explicit getter:

class SomeData(val counter: Int...) {

    private val maxValue: String
        get() = counter / 2

    // some logic
}

The second form also means that if your counter would be a mutable var (instead of an immutable val), then the maxValue would also change when the counter changes.

逆蝶 2025-01-22 18:54:16

常量是在整个执行过程中从未改变的默认值,并且它从不动态依赖于任何其他值。您可以创建如下

companion object { private const val CONSTAN_NAME = "value" }

这本质上是不可变的。通过这种方式声明,您告诉编译器这是一个内联常量 &它将在运行时直接替换为值。因此优化了创建常量的方式。

Constant is something default value which never changed through out the execution and it never depend on any other value dynamically. You can create as below

companion object { private const val CONSTAN_NAME = "value" }

This is immutable in nature & by declaring this way you are telling to compiler that this is a inline constant & which will directly replace with value during run time. So optimized way creating constant.

夕嗳→ 2025-01-22 18:54:16

创建一个文件常量,并在该文件中创建常量

const val DATABASE_NAME = "database-xyz"

,如果您创建一个伴随对象,那么这不是一个好的做法,因为它也会创建该类的实例,并且它将获取资源,直到您在使用后不销毁它为止

create a file Constants and inside that file just create constants like

const val DATABASE_NAME = "database-xyz"

and if you create a companion object then it's not a good practice as it will create instance of that class too and it will get resources until you don't destroy it after usage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文