gpt4 book ai didi

java - Guice AssistedInject 找不到匹配的构造函数

转载 作者:行者123 更新时间:2023-11-30 06:08:46 25 4
gpt4 key购买 nike

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

PlayerFactory.java

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

BlackjackPlayer.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);
}
}

玩家等级

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 辅助注入(inject)来创建 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) 但没有帮助。有人知道我应该做什么来解决这个问题吗?

提前致谢!

最佳答案

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

PlayerFactory.java

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

BlackjackPlayer.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)
);

关于java - Guice AssistedInject 找不到匹配的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50695605/

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