【问题标题】:Unresolved reference: Curry未解决的参考:咖喱
【发布时间】:2021-07-29 11:44:15
【问题描述】:

我想在 SpiceContainer 中打印出香料标签,但我得到了 这是我的代码。我该如何解决?

package Spice
fun main(args: Array<String> ) {
    jack()
}

class Spice(){
    val name:String=("Curry")
    val spiciness:String=("mild")
}
data class SpiceContainer(var spice: Spice){
    val label=spice.name
}

fun jack(){
    val spiceCabinet = listOf(SpiceContainer(Curry("Yellow Curry", "mild")),
            SpiceContainer(Curry("Red Curry", "medium")),
            SpiceContainer(Curry("Green Curry", "spicy")))
    for(element in spiceCabinet) println(element.label)
}

【问题讨论】:

  • 您的代码中没有class Curry(至少在您发布的代码中)。你有一个 class Spice 固定值 name = "Curry" 我怀疑这就是你想要的。你能解释一下你想做什么吗?
  • 我正在尝试打印容器标签。 classroom.udacity.com/courses/ud9011/lessons/… 我正在从 udacity 学习 kotlin,但我猜他们的答案代码既不正确也不清晰也不完整。我重写为 class Spice(val name: String, val spiciness: String){} 并将 SpiceContainer(Curry 更改为 SpiceContainer(Spice 然后它工作正常。
  • 该链接需要一个帐户...我已经有太多了,。所以我拒绝创建另一个。
  • 您可以使用 google acct 登录,就像我使用 google acct 登录本网站一样,无需创建新帐户。如果你不想这样做也没关系。你已经得到了很多帮助。
  • 最好将实际问题发布为文本或至少是其中最重要的部分以及自己的努力。这样,任何人都不必拥有谷歌帐户或任何其他身份。很高兴帮助...感谢您接受答案。如果您对此有足够的声誉,请考虑投票。

标签: kotlin


【解决方案1】:

您正在尝试创建一个您没有的class Curry 实例。相反,您有一个class Spice 和一个name 和一个spiciness。如果你想要一个实例,比如说 Red Curry,你必须实例化一个 Spice 并给它所需的 nameSpice("Red Curry", "kinda hot")
spiciness(至少比 String 更好)的一个好的做法是 enum class Spiciness...

这是一个简单的例子:

// a spice consisting of a name and some degree of spiciness
data class Spice(val name: String, val spiciness: Spiciness)

// the spice container that gets the label of the spice contained
data class SpiceContainer(val spice: Spice) {
    val label: String = spice.name
}

// enumerate possible degrees of spiciness
enum class Spiciness {
    NONE_AT_ALL,
    MILD,
    MODERATE,
    HOT,
    DANGEROUSLY_HOT,
    LIFE_THREATENING,
    UNKNOWN_TRY_YOURSELF
}

fun jack() {
    // create a list of spices and add some arbitrary ones
    val spiceCabinet = listOf(SpiceContainer(Spice("Yellow Curry", Spiciness.MODERATE)),
                              SpiceContainer(Spice("Red Curry", Spiciness.HOT)),
                              SpiceContainer(Spice("Green Curry", Spiciness.UNKNOWN)),
                              SpiceContainer(Spice("Pepper", Spiciness.MILD)))
     // print each item's label
     spiceCabinet.forEach{ it -> println(it.label) }
                              
}

然后像这样运行fun jack()

fun main() {
    jack()
}

为了得到输出

Yellow Curry
Red Curry
Green Curry
Pepper

我真的不明白你为什么需要或想要SpiceContainer,但这超出了这个答案的范围(很可能是问题)。

【讨论】:

  • 您的解决方案非常完美!!!如果我删除 SpiceContainer => val spiceCabinet = listOf(Spice("Yellow Curry", "mild") 那么 println(element.label) 的标签变为红色。警报:未解决的参考:标签,如何解决这个问题?
  • @LisaBaron 该行出现错误,因为您随后拥有Spices 列表,而不是SpiceContainers 列表。您需要将其更改为 element.name,因为 Spicename 实际上是 SpiceContainerlabel 从中获取其价值的。
  • 感谢您抽出宝贵时间回答我的问题。既然你花了时间,你一定认为我的问题有一些价值。你能投票给这个问题吗?这个问题将来也会使其他人受益。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 2021-08-17
  • 2016-12-14
  • 2020-05-27
  • 2019-11-16
  • 2021-01-24
相关资源
最近更新 更多