Guice AsstedInject 找不到匹配的构造函数



我刚开始学习Guice,但我已经遇到了一个问题。我有一个接口PlayerFactory一个实现BlackjackPlayer

播放器工厂.java

public interface PlayerFactory {
Player createPlayer(String name);
Player createPlayer(String name, boolean isDealer);
}

二十一点玩家.java

public class BlackjackPlayer implements PlayerFactory {
private PointsCalculator pointsCalculator;
public BlackjackPlayer(){
pointsCalculator = new BlackjackPointsCalculator();
}
@Override
public Player createPlayer(String name) {
return new Player(pointsCalculator, name);
}
@Override
public Player createPlayer(String name, boolean isDealer) {
return new Player(pointsCalculator, name, isDealer);
}
}

播放器.class

public class Player{
private PointsCalculator pointsCalculator;
private List<Card> cardsInHand;
private Integer points;
private String name;
private boolean isDealer;
private boolean endedTurn;
@AssistedInject
public Player(PointsCalculator blackjackPointsCalculator, String name){
pointsCalculator = blackjackPointsCalculator;
cardsInHand = new ArrayList<>();
points = 0;
this.name = name;
isDealer = false;
endedTurn = false;
}
@AssistedInject
public Player(PointsCalculator blackjackPointsCalculator, String name, boolean isDealer){
pointsCalculator = blackjackPointsCalculator;
cardsInHand = new ArrayList<>();
points = 0;
this.name = name;
this.isDealer = isDealer;
endedTurn = false;
}
public void addCardToHand(Card card) {
cardsInHand.add(card);
updatePoints();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Player)) return false;
Player player = (Player) o;
return getPoints() == player.getPoints() &&
isDealer() == player.isDealer() &&
isEndedTurn() == player.isEndedTurn() &&
Objects.equals(pointsCalculator, player.pointsCalculator) &&
Objects.equals(getCardsInHand(), player.getCardsInHand()) &&
Objects.equals(getName(), player.getName());
}
@Override
public int hashCode() {
return Objects.hash(pointsCalculator, getCardsInHand(), getPoints(), getName(), isDealer(), isEndedTurn());
}
public void updatePoints() {
points = pointsCalculator.calculatePoints(cardsInHand);
}
public List<Card> getCardsInHand(){
return cardsInHand;
}
public Integer getPoints(){
return points;
}
public String getName(){
return name;
}
public boolean isDealer() {
return isDealer;
}
public boolean isEndedTurn() {
return endedTurn;
}
public void setName(String name){
this.name = name;
}
public void setDealer(boolean isDealer){
this.isDealer = isDealer;
}
public void setEndedTurn(boolean endedTurn){
this.endedTurn = endedTurn;
}
}

我想使用 Guice 辅助注射来创建Player。以前我这样做如下:

install(new FactoryModuleBuilder().build(PlayerFactory.class));

我知道这是错误的方式,因为我收到错误消息:

1) com.github.blackjack.model.Player has @AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer().  Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)
2) com.github.blackjack.model.Player has @AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer().  Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)

我试图Player(String name)添加构造函数,Player(String name, boolean isDealer)但没有帮助。有人知道我应该怎么做才能解决问题吗?

提前感谢!

您需要在注入参数上使用@Assisted注释:

播放器工厂.java

public interface PlayerFactory {
Player createPlayer(String name);
Player createPlayer(String name, boolean isDealer);
}

二十一点播放器.java(将其从工厂更改为实际玩家(

public class BlackjackPlayer implements Player {
private final PointCalculator pointsCalculator;
private final String name;
private final boolean isDealer;
@AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, @Assisted String name) {
this.pointsCalculator = pointsCalculator;
this.name = name;
this.isDealer = false;
}
@AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, @Assisted String name, @Assisted boolean isDealer) {
this.pointsCalculator = pointsCalculator;
this.name = name;
this.isDealer = isDealer;
}
}

并按如下方式使用该模块:

install(new FactoryModuleBuilder()
.implement(Player.class, BlackjackPlayer.class)
.build(PlayerFactory.class)
);

相关内容

  • 没有找到相关文章

最新更新