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
The Enum constructor can accept multiple values.
Since Kotlin enums are classes, they can have their own properties, methods, and implement interfaces.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With