I have a enum called scenes and some classes like game and menu, I get class of the enum using id how can I do that? thats all I need. sorry my english is bad.
EDIT:
this is my code
public enum Scenes {
MENU(1, "Menu"),
GAME(2, "Game");
public static int Scene = 1;
private int id;
private String name;
private Scenes (int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
You want to do something like this? To get Scenes object based on it's id?
Scenes scene = Scenes.valueOf(1)
Then that's how you do it. Look at the valueOf method.
public enum Scenes {
MENU(1, "Menu"),
GAME(2, "Game");
public static int Scene = 1;
private int id;
private String name;
private Scenes (int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public static Scenes valueOf(int id) {
for (Scenes value : Scenes.values()) {
if (value.getId() == id) {
return value;
}
}
}
}
But pay attention, that calling values() repetitively is bad for performance. So you probably should build Map<Integer, Scenes> in static initialization block and save the map to a static final field.
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.