我想知道使用自定义参数多次实例化同一对象的最佳方法。 我取得了成就,并取得了成就:
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);
}
但这种方法最优化吗?我可以创建一个类成就,没有枚举吗?感谢您的评论。
答案 0 :(得分:0)
我可以创建一个类成就,没有枚举吗?
当然,为什么不:
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
}
答案 1 :(得分:0)
无需在班级中重复枚举的字段,只需通过(不可变)成就字段(即myAchievement.getAchievementType().getMax()
)访问它们。
public class Achievement implements Serializable{
private static final long serialVersionUID = 1L;
private final Achievements achievementType;
private boolean passed;
private long userId;
private int score;
public Achievement(Achievements achievementType, long userId) {
this.achievementType = achievementType;
this.userId = userId;
this.score = 0;
this.passed = false;
}
// getters
public Achievements getAchievementType() {
return achievements;
}
public long getUserId() {
return userId;
}
...
另外,我会考虑将您的Achievements
枚举重命名为AchievementType
或类似内容。
我不建议制作公开的静态成就实例,这些实例可以从Achievement
类获得,例如Achievement.CHANNELJOIN
,因为看起来你想为每个用户创建一个单独的Achievement实例,并且那些必然是单身人士。