Reputation: 233
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
Upvotes: 23
Views: 26866
Reputation: 97138
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
.
Upvotes: 54