是否可以使用注释使以下代码更简洁,减少重复?我知道使用 java 8 闭包是可能的,但试图让它在 java 6/7 上工作
Variable x = new Variable(this,"HClass","HC"){
@Override
String getValue(Player p){
return getHeroFromPlayer(p).getHeroClass().getName();
}
};
Variable y = new Variable(this,"HSecClass","HSC"){
@Override
String getValue(Player p){
return getHeroFromPlayer(p).getSecondClass().getName();
}
};
Variable z = new Variable(this,"HLevel","HL"){
@Override
String getValue(Player p){
return getHeroFromPlayer(p).getLevel();
}
};
Variable a = new Variable(this,"HMastered","HMa"){
@Override
String getValue(Player p){
return getHeroFromPlayer(p).isMaster(getHeroFromPlayer(p).getHeroClass()) && (heroSClass == null || getHeroFromPlayer(p).isMaster(heroSClass))
? LocaleType.MESSAGE_HEROES_TRUE.getVal() : LocaleType.MESSAGE_HEROES_FALSE.getVal();;
}
};
这会持续一段时间,它们都被添加到地图中,地图会延迟返回结果。
编辑: 我希望注释能让我按照以下方式做一些事情
@Variable("HLevel","HL")
String getHLevel(){getHeroFromPlayer(p).getlevel();}
编辑:变量.java
abstract class Variable {
final private VariableGroup vg;
final private List<String> keys = new Vector<String>();
Variable(VariableGroup vg,String...varargs){
this.vg = vg;
for (String s:varargs){
keys.add(s);
}
}
abstract String getValue(Player p);
}