函数声明必须具有名称和期望成员声明

时间:2019-07-17 10:51:04

标签: kotlin

在密封类Entity中,发生了与前3行有关的一些错误

  1. 科特琳:期待成员声明

  2. 科特琳:期待成员声明

  3. 科特琳:函数声明必须具有名称

enter image description here

在本教程中似乎都正确。 https://youtu.be/F9UC9DY-vIU?t=7451

import java.util.*

interface IdProvider {
    fun getId(): String
}

enum class EntityType {
    HELP, EASY, MEDIUM, HARD;

    fun getFormatedName() = name.toLowerCase().capitalize()
}

object EntityFactory {
    fun create(type: EntityType) : Entity {
        val id = UUID.randomUUID().toString()
        val name = when(type) {
            EntityType.EASY -> type.name // type.name = EASY
            EntityType.HARD -> "Hard"
            EntityType.MEDIUM -> type.getFormatedName() // = Medium
            EntityType.HELP -> type.getFormatedName()

        }

        return when(type) {
            EntityType.EASY -> Entity.Easy(id, name)
            EntityType.MEDIUM -> Entity.Medium(id, name)
            EntityType.HARD -> Entity.Hard(id, name, 2f)
            EntityType.HELP -> Entity.Help
        }
    }
}

// Cant extensionate base sealed class
sealed class Entity {
    object Help : Entity() {
        val name = "Help"
    }

    data class Easy(val id: String, val name: String): Entity()
    data class Medium(val id: String, val name: String): Entity()
    data class Hard(val id: String, val name: String, val multiplier: Float): Entity()
}

fun main() {
    val entity:Entity = EntityFactory.create(EntityType.EASY)
    val msg = when(entity) {
        Entity.Help -> "help class"
        is Entity.Easy -> "easy class"
        is Entity.Medium -> "medium class"
        is Entity.Hard -> "hard class"
    }

    println(msg)
    /*println(entity)

    val mediumEntity = EntityFactory.create(EntityType.MEDIUM)
    println(mediumEntity)*/

}

1 个答案:

答案 0 :(得分:0)

我只是删除了帮助对象,然后重新输入了所有内容。没关系。