简体   繁体   中英

Getter for ENUM values

I'm trying to get a variable, or an enum out of an enum(I'm not sure what to call it).

So this is my enum.

public enum fruits {

//ID 0
banana(0,250, 150f, 400f, "Assassin"),
//ID 1
apple(1, 300f, 200f, 600f, "Fighter");





private float atk, def, hp;
private final String Style;
private int ID;


Ship(int id,  float attack, float defence, float health, String fightStyle) {
    atk = attack;
    def = defence;
    hp = health;
    Style = fightStyle;
    ID = id;

}

public String getLore() {
    return Lore;
}

public float getAtk() {
    return atk;
}

public float getPen() {
    return pen;
}

public float getDef() {
    return def;
}

public float getHp() {
    return hp;
}

public String getStyle() {
    return Style;
}

public int getID() {
    return ID;
}

So in this class I'm trying to use the enum to create a player with certain enum stats. Let's say I make a player and he's aa banana, he would have banana stats and be able to upgrade them.

Here is where I try to do that.

public class Player implements BaseEntity{

Fruitfruit;

//STATS
float atk;
float def;
float hp;


public Player(Fruitfruit) {
    this.fruit = fruit;
    atk = fruit.getAtk();
    def = fruit.getDef();
    hp = fruit.getHp();


}

@Override
public float getHP() {
    return hp;
}

@Override
public float getDef() {
    return def;
}

@Override
public float getAtk() {
    return atk;
}

@Override
public float getPen() {
    return pen;
}

@Override
public int getFruit() {
    return fruit.getID();
}

@Override
public String getFruitType() {
    return fruit.getStyle();
}



@Override
public void setFruit() {
    // TODO Auto-generated method stub

}

I want the player to have banana stats and be able to level up those stats. I also want the player to have a certain fruit. Let's say you upgrade from banana to apple, your stats are equal to the apple.

To refer to an Enum value, simply use:

Fruits.BANANA

For example, you can instantiate a Player using the following line:

final Player player = new Player(Fruits.BANANA);

You can do something like this.

Player

public Fruit getFruit() {
    return fruit;
}

public void upgradePlayer(Fruit fruit) {
    this.fruit = fruit;
    atk = fruit.getAtk();
    def = fruit.getDef();
    hp = fruit.getHp();
}

Sample implementation of upgrade player

if (!player.getFruit().equals(Fruit.apple)) {
    player.upgradePlayer(Fruit.apple);
}

And if you at some point get tired of using enums, concider integer constants:

public static final int FRUIT_BANANA = 0;
public static final int FRUIT_APPLE = 1;

private int fruit = FRUIT_BANANA;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM