我是Java初学者,我有一个问题。假设我们有一个班级:
class Foo {
public String test;
public void someMethod() { }
}
我们有两个此类的实例:bar1
和bar2
。
现在,我如何让bar1.someMethod()
更改bar2.test
的值而不将bar2
作为参数传递给bar
1(不像bar1.someMethod(bar2)
)。要清除:我正在制作一个游戏,您可以在其中轮流移动对象,所以我想在移动对象变为非活动状态并使其他对象处于活动状态之后,因此当时只能移动其中一个对象。谢谢。
答案 0 :(得分:1)
简短的回答:不能轻易做到。
长:
bar1
应该如何知道要操纵哪个其他实例?可能有很多情况(或者只有两个,我不知道您的申请)。
您可以在类Foo
中拥有一个知道另一个实例的(私有)字段。
或者您可以拥有一个知道两个实例的类Foos
。
另一个(丑陋的)解决方案是在Foo
中有两个静态字段但是不鼓励这样做,因为这会阻止你有效地使用两个以上的实例。
答案 1 :(得分:1)
你应该解决这个问题
因为你可能会有一个其他类游乐场(或其他)为你的Foos放一个List
和
将您的方法someMethod()
添加到此类中,您可以在其中处理类Foo
class playground{
List<Foo> foos = new List<Foo>();
void someMethod(Foo a,Foo b){
a.changeTest("");
b.changeTest("");
}
}
在Foo课程中你当然会改变状态
class Foo{
void changeTest(String a){
test = a;
}
}
答案 2 :(得分:1)
我不清楚你究竟是什么意思,但我已经去了。我将向您解释两个类,一个用于MainProgram,另一个用于Foo。
public class Foo {
public String test;
private Foo otherPlayer = null;
public void someMethod() {
}
public void setOtherPlayer(Foo otherPlayer) {
this.otherPlayer = otherPlayer;
}
}
我添加了另一个方法,它将另一个Foo实例作为参数传递,但这是唯一需要它的方法,它是为了设置一个字段(其中它表示 private Foo otherPlayer )在您的类实例中,可以从此实例中的所有其他方法访问。
此外,您会注意到关键字此。 this.variable 表示在调用方法所在的同一实例中的字段(字段在类中但在方法外部)。 otherPlayer 指的是setOtherPlayer方法。
public class MainProgram {
public static void main(String[] args) {
Foo player1 = new Foo();
Foo player2 = new Foo();
player1.setOtherPlayer(player2);
player2.setOtherPlayer(player1);
}
}
然后在你的MainProgram中创建两个Foo实例,然后为两个玩家设置他们正在竞争的玩家。这应该在与球员做其他事情之前完成。
答案 3 :(得分:1)
这是多个玩家的另一个代码
import java.util.Random;
public class Dice {
public int roll() {
return new Random().nextInt(6) + 1;
}
}
import java.awt.event.ActionEvent;
public class Display extends JFrame {
public Display(final Game game) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JButton btnNextMove = new JButton("Do next move");
btnNextMove.setBounds(12, 12, 129, 25);
getContentPane().add(btnNextMove);
setBounds(0, 0, 162, 77);
btnNextMove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
game.doNextMove();
}
});
game.start();
}
}
import java.util.ArrayList;
public class Game {
private ArrayList<Player> players = new ArrayList<Player>();
private Player currentPlayer = null;
public void addPlayer(Player p) {
players.add(p);
}
public void start() {
int playerCount = players.size();
for (int i=0; i<playerCount; i++) {
int previousPlayerIndex = i-1;
int nextPlayerIndex = i+1;
// Makes the players loop round at the edges
if (previousPlayerIndex < 0) previousPlayerIndex += playerCount;
if (nextPlayerIndex >= playerCount) nextPlayerIndex -= playerCount;
Player p = players.get(i);
p.setPreviousPlayer(players.get(previousPlayerIndex));
p.setNextPlayer(players.get(nextPlayerIndex));
}
// Sets current player to the first player
setCurrentPlayer(players.get(0));
}
private void setCurrentPlayer(Player p) {
this.currentPlayer = p;
System.out.println();
System.out.println("It's " + p.playerName + "'s turn to make a move.");
}
public void doNextMove() {
Dice dice = new Dice();
int value = dice.roll();
System.out.println(currentPlayer.playerName + " rolled a " + value);
if (value <= 2) skipPlayer();
else if (value <= 4) reverse();
else if (value <= 6) doNormalMove();
}
public void reverse() {
int playerCount = players.size();
for (int i=0; i<playerCount; i++) {
players.get(i).reverseSiblingPlayers();
}
System.out.println("Play is reversed");
setCurrentPlayer(currentPlayer.getNextPlayer());
}
public void doNormalMove() {
System.out.println("Nothing special happens here");
setCurrentPlayer(currentPlayer.getNextPlayer());
}
public void skipPlayer() {
System.out.println(currentPlayer.getNextPlayer().playerName + " misses a turn");
setCurrentPlayer(currentPlayer.getNextPlayer().getNextPlayer());
}
}
public class MainProgram {
public static void main(String[] args) {
Game game = new Game();
game.addPlayer(new Player("Player 1"));
game.addPlayer(new Player("Player 2"));
game.addPlayer(new Player("Player 3"));
game.addPlayer(new Player("Player 4"));
game.addPlayer(new Player("Player 5"));
game.addPlayer(new Player("Player 6"));
Display display = new Display(game);
display.setVisible(true);
}
}
public class Player {
public final String playerName;
private Player previousPlayer = null;
private Player nextPlayer = null;
public Player(String playerName) {
this.playerName = playerName;
}
public void setPreviousPlayer(Player previousPlayer) {
this.previousPlayer = previousPlayer;
}
public void setNextPlayer(Player nextPlayer) {
this.nextPlayer = nextPlayer;
}
public Player getPreviousPlayer() {
return previousPlayer;
}
public Player getNextPlayer() {
return nextPlayer;
}
public void reverseSiblingPlayers() {
Player previousPlayer = this.previousPlayer;
Player nextPlayer = this.nextPlayer;
this.nextPlayer = previousPlayer;
this.previousPlayer = nextPlayer;
}
}
答案 4 :(得分:0)
也许你可以拥有表演者链。
所以Chain应该是这样的:
public class Performer {
private Performer next;
private boolean active;
public perform() {
if(isActive()) {
switchStates();
// do something meaningul
} else if(next.isActive()) {
next.perform();
}
}
public void setActive(boolean active) {
this.active = active.
}
private boolean isActive() {
return active;
}
private void switchStates() {
boolean currentState = this.active;
next.setActive(currentState );
this.active = !currentState;
}
}
IMPL2:表演者应该做不同的事情
public void perform(Action action) {
if(isActive()) {
// update states and do other basic actions
action.execute(); // executes action on current performer
} else if(next.isActive()) {
next.perform(action);
}
}
perform()
方法,你可以在里面做
任何你想要的。 Action
&#34;具有不同实现的类,以及它
应该传递给 perform()方法(你可以在IMPL2代码段中看到这个)