为了清楚地说明这个问题,我用一个例子来说明。
我有一个名为 Player 的简单类,它包含三个数据成员(“攻击”在这里很重要),并为它们提供了 setter 和 getter。 (现在只有 setAttack() 很重要)。 我有一个“Team”类,它将包含三个 Player 对象,以制作一些 在玩家 field 上进行操作。另外,Team 类有一个简单的方法,名为increaseattack(),该方法会将特定玩家的攻击力增加 1。(立即 当通过其构造函数创建 Team 对象时调用)。 在 Match 类中,我创建了 3 个玩家对象,以及由它们创建的团队对象。现在有趣的事情开始了:在 Team 类中为指定的玩家调用 setAttack() 也会改变 Match 类中最初创建的玩家的攻击!
我知道我在一些基本概念上是错误的,但不知道它是如何正确完成的。你能给我解释一下,为什么会这样吗? 这是三个类的源码:
public class Player {
String name;
int attack;
int defense;
public Player(String name, int attack, int defense){
this.name = name;
this.attack = attack;
this.defense = defense;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
}
public class Team {
private Player player1;
private Player player2;
private Player player3;
public Team(Player p1, Player p2, Player p3){
this.player1 = p1;
this.player2 = p2;
this.player3 = p3;
increaseattack(player1);
}
public void increaseattack(Player pla){
pla.setAttack(pla.getAttack()+1);
}
}
public class Match {
Player player, player2, player3;
Team team, team1;
//static Player player, player2, player3;
//static Team team, team1;
public static void main(String[] args){
new Match();
}
public Match(){
test();
}
public void test() {
player = new Player("Alan", 10, 10);
player2 = new Player("Bob", 10, 12);
player3 = new Player("Cedric", 13, 10);
team = new Team(player,player2,player3); // Creating the team instance based on player instances created here, OK 10+1 = 11
team1 = new Team(player,player2,player3); // new team, hopefully i get the same results, but thats not the case, 11 is base attack, not 10!!!
System.out.println("------------------------");
System.out.println(" " + player.getName() + " " + player.getAttack() + " " + player.getDefense());
System.out.println(" " + player2.getName() + " " + player2.getAttack() + " " + player2.getDefense());
System.out.println(" " + player3.getName() + " " + player3.getAttack() + " " + player3.getDefense());
}
}
这是输出:
------------------------
Alan 12 10
Bob 10 12
Cedric 13 10
这对我来说是不正确的,因为我预期艾伦的原始攻击没有改变,只是“在”团队中。我很感激任何帮助。谢谢。
最佳答案
您创建了一个攻击力为 10 的玩家:
player = new Player("Alan", 10, 10);
然后将该球员添加到两个团队:
team = new Team(player,player2,player3);
team1 = new Team(player,player2,player3);
Team
的构造函数中发生了什么?:
this.player1 = p1;
//...
increaseattack(player1);
increaseattack()
是做什么的?:
pla.setAttack(pla.getAttack()+1);
因此,Alan
的攻击值从原来的 10 变为新值 12,因为最后一个操作对其执行了两次,为他加入的每个团队执行一次。
编辑:根据您下面的评论,听起来您想要做的不是询问玩家他们的攻击是什么,而是询问团队给定玩家的攻击是什么。这是因为“第一个”玩家的“攻击修饰符”是一个团队数据元素,而不是玩家数据元素。
因此,也许在 Team
类中,您可能会看到类似的内容:
public int getAttackFor(Player player) {
if (player == player1) {
return player.getAttack() + 1;
}
if ((player == player2) || (player == player3)) {
return player.getAttack();
}
// throw an exception if the player isn't found?
}
这只是一个快速的徒手想法,您可以根据自己的需要进行调整。也许基于玩家标识符进行比较,而不是直接对象引用比较,也许将玩家放入数组中,而不是与单个变量进行比较,等等。但想法是:
- 提供一个
玩家
并询问该玩家对该团队的攻击(因为它显然可能与玩家单独的攻击不同) - 确定
玩家
是否遭受“修改过的”攻击 - 返回该
玩家
该团队的攻击。
因此,要使用它,您需要在 main()
中执行类似的操作:
System.out.println(" " + player.getName() + " " + team.getAttackFor(player) + " " + player.getDefense());
关于java - 如果它们在组合中,如何从另一个类中正确调用java中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233541/