代码之家  ›  专栏  ›  技术社区  ›  Prithvi Bhola

kotlin中的枚举

  •  1
  • Prithvi Bhola  · 技术社区  · 6 年前

    keys 为了一个 item 在里面 enum class

    enum class Events {
            REFER_AND_EARN {
                val key: String = "Refer and Earn"
                val source: String = "Source"
            },
            REFILL_PAST_MEDICINE_CLICK {
                val key: String = "Refill Past Medicine Click"
                val source: String = "Source"
                val pointOfInitiation: String = "Point of initiation"
            }
        }
    

    枚举类 我能进入吗 source 这样地??

    Events.REFER_AND_EARN.source

    3 回复  |  直到 6 年前
        1
  •  3
  •   Dominik G.    6 年前

    你可以通过写这篇文章来达到你想要的目的:

    enum class Events(val key: String, val source: String, val pointOfInitiation: String? = null) {
        REFER_AND_EARN(key = "Refer and Earn", source = "Source"),
        REFILL_PAST_MEDICINE_CLICK(
            key = "Refill Past Medicine Click",
            source = "Source",
            pointOfInitiation = "Point of initiation"
        )
    }
    
        2
  •  1
  •   Miha_x64    6 年前

    可以通过枚举实现接口,并通过重写接口中的属性来公开这些属性。

    或者你可以申报 sealed class 而不是 enum class 使用 object declarations

        3
  •  1
  •   Hexworks Hemantkumar Gaikwad    6 年前

    您需要改用属性:

    enum class Events(val key: String,
                      val source: String, 
                      val pointOfInitiation: String) {
    
            REFER_AND_EARN("Refer and Earn",
                 "Source",
                 "Unknown"),
            REFILL_PAST_MEDICINE_CLICK(
                "Refill Past Medicine Click",
                "Source",
                "Point of initiation"
            );
    }
    

    sealed class 正如其他人提到的。