23

For an exercise I have an enum (set by the teacher) that looks like this :

enum class Weapon(name: String, damage: Int) {
    SWORD("Sword", 12),
    AXE("Axe", 13),
    BOW("Bow", 14)
}

The weapon will be an attribute of a data class Player
But once I set player.weapon = Weapon.SWORD
How do i access to the name or damage of the weapon ?

I have looked on the Internet for an answer but didn't find anywhere an enum with two "parameter" (don't know how to call it) so I start wondering if this enum is possible.

Thanks guys

1 Answer 1

55

As shown in the documentation, you need to declare the name and damage as properties of the enum class, using the val keyword:

enum class Weapon(val weaponName: String, val damage: Int)

Then you'll be able to simply access player.weapon.weaponName.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh yeah and I can't call it name, as there already a name member in Enum. Thank you for your quick answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.