gpt4 book ai didi

java - 枚举 RPS 实现在初始化程序和非法前向引用中提供自引用

转载 作者:行者123 更新时间:2023-11-30 11:20:55 27 4
gpt4 key购买 nike

我有以下代码,我相信我明白为什么我会收到错误:

错误是,例如:

  • 初始化器中的自引用 RPSGesture.ROCKROCK 的构造函数。
  • RPSGesture.PAPERROCK 构造函数中存在非法前向引用

public interface Gesture {
public List<? extends Gesture> winsFrom();

public List<? extends Gesture> tiesTo();

public List<? extends Gesture> losesTo();
}

public enum RPSGesture implements Gesture, Action {
ROCK(
Arrays.asList(RPSGesture.SCISSORS),
Arrays.asList(RPSGesture.ROCK),
Arrays.asList(RPSGesture.PAPER)
),

PAPER(
Arrays.asList(RPSGesture.ROCK),
Arrays.asList(RPSGesture.PAPER),
Arrays.asList(RPSGesture.SCISSORS)
),

SCISSORS(
Arrays.asList(RPSGesture.PAPER),
Arrays.asList(RPSGesture.SCISSORS),
Arrays.asList(RPSGesture.ROCK)
);

private final List<RPSGesture> winsFrom;
private final List<RPSGesture> tiesTo;
private final List<RPSGesture> losesTo;

private RPSGesture(final List<RPSGesture> winsFrom, final List<RPSGesture> tiesTo, final List<RPSGesture> losesTo) {
this.winsFrom = winsFrom;
this.tiesTo = tiesTo;
this.losesTo = losesTo;
}

@Override
public List<RPSGesture> winsFrom() {
return winsFrom;
}

@Override
public List<RPSGesture> tiesTo() {
return tiesTo;
}

@Override
public List<RPSGesture> losesTo() {
return losesTo;
}
}

我看过Peter Lawrey's Answer ,但是 static 初始化程序真的是最好的方法吗?还有其他合理的选择吗?

这个枚举的设计看起来是否正确,或者您自己会做不同的设计吗?希望类中的代码更少。

最佳答案

我自己找到了一个合适的方法,使用 Java 8:

public interface Gesture {
public List<? extends Gesture> winsFrom();

public List<? extends Gesture> tiesTo();

public List<? extends Gesture> losesTo();
}

public enum RPSGesture implements Gesture, Action, RuleGestureFPSFactory {
ROCK,
PAPER,
SCISSORS;

@Override
public List<RPSGesture> winsFrom() {
return winMapping().get(this);
}

@Override
public List<RPSGesture> tiesTo() {
return tieMapping().get(this);
}

@Override
public List<RPSGesture> losesTo() {
return loseMapping().get(this);
}
}

public interface RuleGestureFactory<T extends Gesture> {
public Map<T, List<T>> winMapping();

public Map<T, List<T>> tieMapping();

public Map<T, List<T>> loseMapping();
}

public interface RuleGestureFPSFactory extends RuleGestureFactory<RPSGesture> {
@Override
default public Map<RPSGesture, List<RPSGesture>> winMapping() {
Map<RPSGesture, List<RPSGesture>> mapping = new HashMap<>();
mapping.put(ROCK, Arrays.asList(SCISSORS));
mapping.put(PAPER, Arrays.asList(ROCK));
mapping.put(SCISSORS, Arrays.asList(PAPER));
return mapping;
}

@Override
default public Map<RPSGesture, List<RPSGesture>> tieMapping() {
Map<RPSGesture, List<RPSGesture>> mapping = new HashMap<>();
mapping.put(ROCK, Arrays.asList(ROCK));
mapping.put(PAPER, Arrays.asList(PAPER));
mapping.put(SCISSORS, Arrays.asList(SCISSORS));
return mapping;
}

@Override
default public Map<RPSGesture, List<RPSGesture>> loseMapping() {
Map<RPSGesture, List<RPSGesture>> mapping = new HashMap<>();
mapping.put(ROCK, Arrays.asList(PAPER));
mapping.put(PAPER, Arrays.asList(SCISSORS));
mapping.put(SCISSORS, Arrays.asList(ROCK));
return mapping;
}
}

这样您甚至可以轻松地实现不同的规则,但它不支持在运行时切换。

关于java - 枚举 RPS 实现在初始化程序和非法前向引用中提供自引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22500338/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com