在枚举中实现内部接口时的循环继承
我有以下实现,给出编译器错误:
public enum FusionStat implements MonsterStatBuilderHelper {
ATTACK {
@Override
public MonsterCard.MonsterCardBuilder safeCreateBuilder(final MonsterCard baseMonsterCard, final MonsterCard fusedMonsterCard, final FusionCard fusionCard) {
Objects.requireNonNull(baseMonsterCard);
Objects.requireNonNull(fusedMonsterCard);
Objects.requireNonNull(fusionCard);
if (baseMonsterCard.equals(fusedMonsterCard)) {
throw new IllegalArgumentException("baseMonsterCard and fusedMonsterCard need to be different");
}
return new MonsterCard.MonsterCardBuilder(baseMonsterCard)
.attack(baseMonsterCard.getAttack() + (fusionCard.getFusionPower() * fusedMonsterCard.getAttack()));
}
},
HITPOINTS {
@Override
public MonsterCard.MonsterCardBuilder safeCreateBuilder(final MonsterCard baseMonsterCard, final MonsterCard fusedMonsterCard, final FusionCard fusionCard) {
Objects.requireNonNull(baseMonsterCard);
Objects.requireNonNull(fusedMonsterCard);
Objects.requireNonNull(fusionCard);
if (baseMonsterCard.equals(fusedMonsterCard)) {
throw new IllegalArgumentException("baseMonsterCard and fusedMonsterCard need to be different");
}
return new MonsterCard.MonsterCardBuilder(baseMonsterCard)
.maximumHitpoints((int)(baseMonsterCard.getMaximumHitpoints() + (fusionCard.getFusionPower() / 100d * fusedMonsterCard.getMaximumHitpoints())))
.hitpoints((int)(baseMonsterCard.getHitpoints() + (fusionCard.getFusionPower() / 100d * fusedMonsterCard.getHitpoints())));
}
};
protected interface MonsterStatBuilderHelper extends MonsterStatBuilder {
default MonsterCard.MonsterCardBuilder safeCreateBuilder(final MonsterCard baseMonsterCard, final MonsterCard fusedMonsterCard, final FusionCard fusionCard) {
return createBuilder(baseMonsterCard, fusedMonsterCard, fusionCard);
}
}
}
@FunctionalInterface
interface MonsterStatBuilder {
MonsterCard.MonsterCardBuilder createBuilder(final MonsterCard baseMonsterCard, final MonsterCard fusedMonsterCard, final FusionCard fusionCard);
}
它在第一行给出循环继承错误。involving FusionStat
我不完全明白发生了什么。我首先实现了一个抽象类,并希望让枚举扩展它,直到我意识到枚举不能扩展类。现在我尝试(ab)使用Java 8中的默认方法。
我对为什么我的代码不编译的思考过程感兴趣,我试图通过拉取重复的代码来删除代码重复(仍然必须这样做)。safeCreateBuilder