一个实例如何更改相同 class 的另一个实例的某些变量

How can one instance change some variable of the other instance of the same class

我是 Java 初学者,我有一个问题。假设我们有一个 class:

class Foo {
    public String test;

    public void someMethod() { } 
} 

我们有两个 class 实例:bar1bar2

现在我怎样才能让 bar1.someMethod() 改变 bar2.test 的值而不将 bar2 作为参数传递给 bar1(没有像 bar1.someMethod(bar2)).清除:我正在制作一个可以轮流移动对象的游戏,所以我希望在移动对象变为非活动状态并使其他对象处于活动状态之后,这样一次只能移动其中一个对象。谢谢。

简答:不容易做到。

更长: bar1 应该如何知道要操作哪个其他实例?可能有很多实例(或只有两个,我不知道你的应用程序)。

您可以在 class Foo 中有一个(私有)字段,它知道另一个实例。 或者你可以有一个 class Foos 知道这两个实例。 另一个(丑陋的)解决方案是在 Foo 中有两个静态字段,但是不鼓励这样做,因为这将阻止您有效地使用两个以上的实例。

你应该以不同的方式解决这个问题

因为你可能会有另一个 class 游乐场(或其他)放在那里你的 Foos 和 将您的方法 someMethod() 添加到此 Class 中,您可以在其中处理 class Foo

的变量测试状态
class playground{

  List<Foo> foos = new List<Foo>();

 void someMethod(Foo a,Foo b){
    a.changeTest("");
    b.changeTest("");
  }
}

而在 class Foo 中,你当然会真正改变状态

class Foo{

  void changeTest(String a){
    test = a;
  }
}

我不太清楚你的意思,但我试过了。我将用两个 class 向您解释,一个用于您的 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) 在你的 class 实例中,可以从这个实例中的所有其他方法访问。

此外,您会注意到关键字 thisthis.variable 表示与调用方法所在实例相同的字段(字段在 class 内但在方法外)。 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 的两个实例,然后为两个玩家设置他们竞争的玩家。这应该在对玩家进行任何其他操作之前完成。

也许你可以拥有你的表演者链。

所以链应该是这样的:

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() 方法中调用它 根据表演者“活跃”状态。所以,当某些事件发生时 注册,你应该调用 perform() 方法,你可以在里面做 无论你想要什么。
  • 如果你的表演者应该做不同的事情,你应该考虑 关于具有不同实现的“Action”class,它 应该传递给 perform() 方法(你可以在 IMPL2 片段中看到这个)

这是多人游戏的另一个代码

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;
    }
}