我已经在我的main方法中初始化了这个Game()构造函数,它将2个接口作为参数:
Game game = new Game(
(player, bridge) -> {
if (player instanceof HumanPlayer)
return true;
if (player instanceof RobotPlayer && bridge instanceof FakeBridge)
return true;
return false;
},
(player) -> {
if (player instanceof HumanPlayer) {
HumanPlayer hPlayer = (HumanPlayer) player;
hPlayer.updateAge(hPlayer.age() + 1);
Car car = (Car) hPlayer.vehicle();
car.setGas(car.gas());
} else if (player instanceof RobotPlayer) {
RobotPlayer rPlayer = (RobotPlayer) player;
rPlayer.updatePower(rPlayer.power() - 5);
}
System.out.println(player);
}
);
以下是我在Game()
的构造函数中提供的两个接口public Initialize initial;
public Interface2 player1;
这是Game类的构造函数:
public Game(Initialize initial, Interface2 player ) {
this.initial = initial;
this.player = player1;
}
由于某种原因,它仍然给我一个错误: 构造函数游戏((玩家,桥牌) - > {},(玩家) - > {})未定义
编辑:这是我提供的两个接口:
public interface Initialize {
boolean constraint(Player player, Bridge bridge);
}
和
public interface Interface2 {
public boolean constraint2(Player player);
}
答案 0 :(得分:2)
您的问题是关于签名与界面不匹配的第二个声明:此编译:
public void run() {
Game game = new Game((player, bridge) -> {
return true;
}, (player) -> {
return true;
});
}
public class Bridge {
}
public class Player {
}
public interface Initialize {
boolean constraint(Player player, Bridge bridge);
}
public interface Interface2 {
boolean constraint(Player player);
}
public class Game {
private Initialize initial;
private Interface2 player;
public Game(Initialize initial, Interface2 player) {
this.initial = initial;
this.player = player;
}
}
你错过了在你的第二个声明上有一个返回类型
答案 1 :(得分:1)
您错过了函数(player) -> {...}
的返回语句,但如果您不想返回值,请使用Consumer界面。
尽管如此,我建议您使用interfaces already in place而不是创建自己的完整副本。