简体   繁体   English

从 ZD52387880E1EA22817A72D3759218 访问嵌套在伴侣 object 中的 Kotlin object

[英]Accessing a Kotlin object nested in companion object from Java

I have a structure like this in Kotlin我在 Kotlin 有这样的结构

companion object Constants {
    /**
     * Collection of fields and values relative to phone books.
     */
    object PhoneBooks {
        /**
         * Field indicating the ID of a phone book.
         * Each phone book must have an unique ID.
         */
        const val PB_ID_KEY = "PB_ID"

        /**
         * Field indicating the status of phone book.
         */
        const val PB_STATUS_KEY = "PB_Status"

        /**
         * One of the possible [PB_STATUS_KEY] values, when the phone book is in indexing state
         * (usually at startup or in update phase).
         */
        const val PB_INDEXING = "Indexing"
        [...]

The problem is that I must have the possibility to access the constants' values in sub-objects from Java, but it seems not possible.问题是我必须有可能从 Java 访问子对象中的常量值,但这似乎不可能。 How can I solve that without changing the structure?我怎样才能在不改变结构的情况下解决这个问题?

JB Nizet commented: JB Nizet评论道:

Works fine here using在这里工作正常

import static com.yourcompany.yourproject.YourClass.Constants.PhoneBooks;

and then接着

PhoneBooks.PB_ID_KEY

It works very good for me.它对我很有用。 So I think it's useful to make it visible as an answer.所以我认为让它作为答案可见是有用的。

In the comments above JB Nizet shows how to solve the issue with a static import在上面的评论中,JB Nizet 展示了如何使用 static 导入来解决问题

However Looking at the provided code I would use an Enum但是查看提供的代码我会使用枚举

// kotlin
enum class PhoneBooks(val param:String) {
    PB_ID_KEY("PB_ID"),
    PB_STATUS_KEY("PB_Status"),
    PB_INDEXING("Indexing")

}

// java
System.out.println(PhoneBooks.PB_ID_KEY.getParam());

A big advantage here is code readability PhoneBooks.PB_ID_KEY flags PB_ID_KEY as a phone book constant in a clean way这里的一大优势是代码可读性 PhoneBooks.PB_ID_KEY 以干净的方式将 PB_ID_KEY 标记为电话簿常量

like Kotlin sealed classes the kolin compiler add some nice checks for enums (exhaustive) and they are designed to provide clean readable code for pattern matching像 Kotlin 密封类一样,kolin 编译器为枚举添加了一些很好的检查(详尽),它们旨在为模式匹配提供干净可读的代码

see @Rolands answer here Kotlin: Using enums with when请参阅@Rolands 在这里回答Kotlin: Using enums with when

try using interface:)尝试使用界面:)

  companion object Constants {
/**
 * Collection of fields and values relative to phone books.
 */
interface PhoneBooks {
    /**
     * Field indicating the ID of a phone book.
     * Each phone book must have an unique ID.
     */
    const val PB_ID_KEY = "PB_ID"

    /**
     * Field indicating the status of phone book.
     */
    const val PB_STATUS_KEY = "PB_Status"

    /**
     * One of the possible [PB_STATUS_KEY] values, when the phone book is in indexing state
     * (usually at startup or in update phase).
     */
    const val PB_INDEXING = "Indexing"
    [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM