首页 > 解决方案 > 从kotlin中的数据类中获取参数的值

问题描述

所以,我对 kotlin 还很陌生,还在学习东西,我有一个名为 Country 的数据类,有 4 个参数

县(名称:字符串,警察电话:字符串,救护车电话:字符串,消防队电话:字符串)

,其中包含 27 个对象的 listOf Country 和从 MainActivity 获取的 var nameC1。我已经调用了 forEach 的列表方法,我想用变量 nameC 来处理列表中的每个名称,并在找到匹配项时执行一些代码。

data class Country(val name: String, val police:String, val ambulance:String,val firefighter:String) {

}

    var nameC1 = (activity as MainActivity).nameC
    val numberList= listOf<Country>(
        Country("Austria","133","144","122"),
        Country("Belgium","101","100","100"),
        Country("Bulgaria","166","150","160"),
        Country("Croatia","192","194","193"),
        Country("Cyprus","199","199","199"),
        Country("Czech Republic","158","155","150"),
        Country("Denmark","112","112","112"),
        Country("Estonia","112","112","112"),
        Country("Finland","112","112","112"),
        Country("France","17","15","18"),
        Country("Germany","110","112","112"),
        Country("Greece","100","166","199"),
        Country("Hungary","107","104","105"),
        Country("Ireland","112","112","112"),
        Country("Italy","113","118","115"),
        Country("Latvia","112","112","112"),
        Country("Lithuania","02","03","01"),
        Country("Luxembourg","113","112","112"),
        Country("Malta","112","112","112"),
        Country("Netherlands","112","112","112"),
        Country("Poland","997","999","998"),
        Country("Portugal","112","112","112"),
        Country("Romania","112","112","112"),
        Country("Slovakia","158","155","150"),
        Country("Slovenia","113","112","112"),
        Country("Spain","092","061","080"),
        Country("Sweden","112","112","112") 
    )

    numberList.forEach { if (Country.name==nameC1 ) }
    // i'm expecting String1==String2 but i'm
    //stuck here because it says name is an unresolved reference 
}

我会使用 getName() 但我知道在 kotlin 中 getter/setter 是自动化的(我不习惯)并且我在 kotlin doc 上没有发现任何有用的东西。网站,我在这个网站上看到有人建议实现 Kotlin-reflection,但我不明白默认情况下我不应该从类中获取参数。

标签: androidkotlin

解决方案


forEach为集合中的每个元素创建一个 lambda。内部元素的默认名称lambdait. 但是您也可以将其重命名为其他名称。参考文档

这是您的代码的工作示例

data class Country(val name: String, val police:String, val ambulance:String,val firefighter:String)

fun doThis(nameC1: String) {
    val numberList= listOf<Country>(
        Country("Austria","133","144","122"),
        Country("Belgium","101","100","100"),
        Country("Bulgaria","166","150","160"),
        Country("Croatia","192","194","193"),
        Country("Cyprus","199","199","199"),
        Country("Czech Republic","158","155","150"),
        Country("Denmark","112","112","112"),
        Country("Estonia","112","112","112"),
        Country("Finland","112","112","112"),
        Country("France","17","15","18"),
        Country("Germany","110","112","112"),
        Country("Greece","100","166","199"),
        Country("Hungary","107","104","105"),
        Country("Ireland","112","112","112"),
        Country("Italy","113","118","115"),
        Country("Latvia","112","112","112"),
        Country("Lithuania","02","03","01"),
        Country("Luxembourg","113","112","112"),
        Country("Malta","112","112","112"),
        Country("Netherlands","112","112","112"),
        Country("Poland","997","999","998"),
        Country("Portugal","112","112","112"),
        Country("Romania","112","112","112"),
        Country("Slovakia","158","155","150"),
        Country("Slovenia","113","112","112"),
        Country("Spain","092","061","080"),
        Country("Sweden","112","112","112") )

    numberList.forEach { 
        if (it.name == nameC1) {
            println("Match")
        }
    }
}

fun main() {
    doThis("Slovenia")
}

在 play.kotlinlang.org 上亲自尝试 -链接

上述代码将println在条件为 时执行该函数true


推荐阅读