我想知道使用自定义参数多次实例化同一对象的最佳方法。 我正在取得成就,取得了一流的成就:
public class Achievement implements Serializable{
private static final long serialVersionUID = 1L;
private Achievements achievement;
private boolean passed;
private final boolean showable;
private int score, multiplicator, max;
private String name, description;
private long userID;
public Achievement(Achievements achievement) {
this.achievement = achievement;
this.max = achievement.getMax();
this.multiplicator = achievement.getMultiplicator();
this.name = achievement.getName();
this.description = achievement.getDescription();
this.showable = achievement.isShowable();
}
还有一个枚举,它有很多参数
public enum Achievements implements Serializable{
CHANNELJOIN("Premier contact vocal", "Rejoindre un channel vocal", 1, 1, true),
FRIEND2("Un ami ?", "Être à 2 dans un channel vocal", 1, 1, true),
FRIEND5("Copains !", "Être à 5 dans un channel vocal", 1, 1, false),
FRIEND8("Monopole d'amis", "Être à 8 dans un channel vocal", 1, 1, false),
CHANNELTIME1("Parleur", "Passer 1 heure dans un channel vocal, non seul", 1, 60, false),
CHANNELTIME2("Parleur 2", "Passer 2 heures dans un channel vocal, non seul", 2, 60, false),
COMMAND1("Première découverte", "Faire une commande du bot", 1, 1, true),
COMMAND2("Commande 2", "Faire 10 commandes avec le bot", 10, 1, true);
private String name;
private String description;
private int max, multiplicator;
private boolean showable;
Achievements(String name, String description, int max, int multiplicator, boolean showable) {
this.name = name;
this.description = description;
this.max = max;
this.multiplicator = multiplicator;
this.showable = showable;
}
(不要介意内容)。所以我正在做的是:
for(Achievements a : Achievements.values()){
Achievement ach = new Achievement(a);
}
但是这个方法是最优化的吗?我可以只创建一个类成就而不使用枚举吗?感谢您的评论。
最佳答案
Can I create just one class Achievement, without enum?
当然,为什么不呢:
public class Achievement {
public static final Achievement CHANNELJOIN = new Achievement("Premier contact vocal", "Rejoindre un channel vocal", 1, 1, true);
public static final Achievement FRIEND2 = new Achievement("Un ami ?", "Être à 2 dans un channel vocal", 1, 1, true);
public static final Achievement FRIEND5 = new Achievement("Copains !", "Être à 5 dans un channel vocal", 1, 1, false);
public static final Achievement FRIEND8 = new Achievement("Monopole d'amis", "Être à 8 dans un channel vocal", 1, 1, false);
public static final Achievement CHANNELTIME1 = new Achievement("Parleur", "Passer 1 heure dans un channel vocal, non seul", 1, 60, false);
public static final Achievement CHANNELTIME2 = new Achievement("Parleur 2", "Passer 2 heures dans un channel vocal, non seul", 2, 60, false);
public static final Achievement COMMAND1 = new Achievement("Première découverte", "Faire une commande du bot", 1, 1, true);
public static final Achievement COMMAND2 = new Achievement("Commande 2", "Faire 10 commandes avec le bot", 10, 1, true);
private boolean passed;
private final boolean showable;
private int score, multiplicator, max;
private String name, description;
private long userID;
public Achievement(String name, String description, int max, int multiplicator, boolean showable) {
this.name = name;
this.description = description;
this.max = max;
this.multiplicator = multiplicator;
this.showable = showable;
}
// more methods here, e.g. getters and setters
}
关于Java 多实例(如枚举),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49739190/