Kotlin - 如果对象的名称存储在字符串中,则访问类的对象

发布于 2025-01-12 15:36:39 字数 919 浏览 1 评论 0原文

我有一个类,它有多个伴随对象,它们是字符串列表。 在另一个类(活动)中,我有一个字符串,它可以具有任何伴随对象的名称。有没有一种方法可以在不使用 If/Else 语句的情况下访问其他类的伴生对象。

DataLists

class ProjectDataLists {
    companion object {
        var Countries: List<String> = listOf(
          "USA",
          "Canada",
          "Australia",
          "UK"
        )

        var Cars: List<String> = listOf(
          "Toyota",
          "Suzuki",
          "Honda",
          "Ford"
        )
    }
}

Activity Class

var IntentVariable: String = "Countries" //This is an Intent variable (extra) from another activity
var DataToBeFetched : List<String>? = null
if (IntentVariable == "Countries")
{
  DataToBeFetched = ProjectDataLists.Countries
}
else if (IntentVariable == "Cars")
{
  DataToBeFetched = ProjectDataLists.Cars
}

我希望 Activity 类的最后一部分在没有 if/else 的情况下完成

I have a class which is having multiple companion objects which are lists of strings.
In another class (Activity) I am having a string which can have name of any of the companion object. Is there a way of accessing other class's companion object without If/Else statements.

DataLists

class ProjectDataLists {
    companion object {
        var Countries: List<String> = listOf(
          "USA",
          "Canada",
          "Australia",
          "UK"
        )

        var Cars: List<String> = listOf(
          "Toyota",
          "Suzuki",
          "Honda",
          "Ford"
        )
    }
}

Activity Class

var IntentVariable: String = "Countries" //This is an Intent variable (extra) from another activity
var DataToBeFetched : List<String>? = null
if (IntentVariable == "Countries")
{
  DataToBeFetched = ProjectDataLists.Countries
}
else if (IntentVariable == "Cars")
{
  DataToBeFetched = ProjectDataLists.Cars
}

I want last part of Activity class to be done without if/else

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

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

发布评论

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

评论(1

还给你自由 2025-01-19 15:36:39

您可以使用反射:

import kotlin.reflect.full.companionObject
import kotlin.reflect.full.memberProperties

class ProjectDataLists {
  companion object {
    var Countries: List<String> = listOf("USA", "Canada", "Australia", "UK")
    var Cars: List<String> = listOf("Toyota", "Suzuki", "Honda", "Ford")
  }
}

var intentVariable = "Countries"

val dataToBeFetched = (ProjectDataLists::class
  .companionObject!!
  .memberProperties
  .first { it.name == intentVariable }   // Will crash with NoSuchElementException if intentVariable is not existing
  .call(ProjectDataLists) as List<*>)
  .filterIsInstance<String>()

println(dataToBeFetched)   // Output: [USA, Canada, Australia, UK]

但是如果您的伴生对象中只有几个列表,我建议不要使用反射。请注意,您可以使用 when 来简化您的 if 语句:

val dataToBeFetched = when (intentVariable) {
  "Countries" -> ProjectDataLists.Countries
  "Cars"      -> ProjectDataLists.Cars
  // and more here ...
}

这非常易读,并且与反射相比非常明确和安全。

You could use reflection:

import kotlin.reflect.full.companionObject
import kotlin.reflect.full.memberProperties

class ProjectDataLists {
  companion object {
    var Countries: List<String> = listOf("USA", "Canada", "Australia", "UK")
    var Cars: List<String> = listOf("Toyota", "Suzuki", "Honda", "Ford")
  }
}

var intentVariable = "Countries"

val dataToBeFetched = (ProjectDataLists::class
  .companionObject!!
  .memberProperties
  .first { it.name == intentVariable }   // Will crash with NoSuchElementException if intentVariable is not existing
  .call(ProjectDataLists) as List<*>)
  .filterIsInstance<String>()

println(dataToBeFetched)   // Output: [USA, Canada, Australia, UK]

But if you have only a few lists in your companion object, I would recommend to not use reflection. Note that you can simplify your if statement by using when:

val dataToBeFetched = when (intentVariable) {
  "Countries" -> ProjectDataLists.Countries
  "Cars"      -> ProjectDataLists.Cars
  // and more here ...
}

This is very readable and in comparison to reflection very explicit and safe.

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