所以我正在看一些网络代码的例子,我无法弄清楚这是做什么的。我假设它在给定的类中创建了一个新变量,但我自己似乎无法做到这一点。有人可以向我解释它的作用吗?
实际行是:
Game.Player playerX = game.new Player(listener.accept(), 'X');
Game
是程序中的另一个类,Player
是一个线程类。我想知道Game.Player playerX
和game.new Player
是做什么的。我将在下面粘贴其余的相关代码。
主要课程:
public class TicTacToeServer {
public static void main(String[] args) throws Exception {
ServerSocket listener = new ServerSocket(8901);
System.out.println("Tic Tac Toe Server is Running");
try {
while (true) {
Game game = new Game();
Game.Player playerX = game.new Player(listener.accept(), 'X');
Game.Player playerO = game.new Player(listener.accept(), 'O');
playerX.setOpponent(playerO);
playerO.setOpponent(playerX);
game.currentPlayer = playerX;
playerX.start();
playerO.start();
}
} finally {
listener.close();
}
}
}
游戏课程:
class Game {
private Player[] board = {
null, null, null,
null, null, null,
null, null, null};
Player currentPlayer;
public boolean hasWinner() {
return
(board[0] != null && board[0] == board[1] && board[0] == board[2])
||(board[3] != null && board[3] == board[4] && board[3] == board[5])
||(board[6] != null && board[6] == board[7] && board[6] == board[8])
||(board[0] != null && board[0] == board[3] && board[0] == board[6])
||(board[1] != null && board[1] == board[4] && board[1] == board[7])
||(board[2] != null && board[2] == board[5] && board[2] == board[8])
||(board[0] != null && board[0] == board[4] && board[0] == board[8])
||(board[2] != null && board[2] == board[4] && board[2] == board[6]);
}
public boolean boardFilledUp() {
for (int i = 0; i < board.length; i++) {
if (board[i] == null) {
return false;
}
}
return true;
}
public synchronized boolean legalMove(int location, Player player) {
if (player == currentPlayer && board[location] == null) {
board[location] = currentPlayer;
currentPlayer = currentPlayer.opponent;
currentPlayer.otherPlayerMoved(location);
return true;
}
return false;
}
class Player extends Thread {
char mark;
Player opponent;
Socket socket;
BufferedReader input;
PrintWriter output;
public Player(Socket socket, char mark) {
this.socket = socket;
this.mark = mark;
try {
input = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
output = new PrintWriter(socket.getOutputStream(), true);
output.println("WELCOME " + mark);
output.println("MESSAGE Waiting for opponent to connect");
} catch (IOException e) {
System.out.println("Player died: " + e);
}
}
public void setOpponent(Player opponent) {
this.opponent = opponent;
}
public void otherPlayerMoved(int location) {
output.println("OPPONENT_MOVED " + location);
output.println(
hasWinner() ? "DEFEAT" : boardFilledUp() ? "TIE" : "");
}
public void run() {
try {
output.println("MESSAGE All players connected");
if (mark == 'X') {
output.println("MESSAGE Your move");
}
while (true) {
String command = input.readLine();
if (command.startsWith("MOVE")) {
int location = Integer.parseInt(command.substring(5));
if (legalMove(location, this)) {
output.println("VALID_MOVE");
output.println(hasWinner() ? "VICTORY"
: boardFilledUp() ? "TIE"
: "");
} else {
output.println("MESSAGE ?");
}
} else if (command.startsWith("QUIT")) {
return;
}
}
} catch (IOException e) {
System.out.println("Player died: " + e);
} finally {
try {socket.close();} catch (IOException e) {}
}
}
}
}
另外:为什么Game
不是public
或private
?它只是class
。
答案 0 :(得分:2)
您正在实例化inner class。基本上这是一个包含在另一个类中的类。为了实例化这样的类,首先需要外部类的实例(在您的情况下为Game
)。
应该注意的是,通常你不希望这样:内部类意味着在外部类中提供额外的封装感。它是一个内部类,原因是:内容应该包含在该外部类中。通过将其暴露给(部分)外部世界,这种意图有点失败。因此,内部类通常是private
。
外部类的构造函数:
Game game = new Game();
内部类的构造函数,使用外部类'instance:
Game.Player playerX = game.new Player(listener.accept(), 'X');
另外:为什么游戏不公开或私密?这只是上课。
为什么呢?我不知道为什么他们在这种特殊情况下确实如此,但这绝对是可能的。创建一个类private
没有多大意义,除非它是一个内部类。如果不是,则无论如何都无法从任何地方访问。
答案 1 :(得分:1)
请注意,它不是Game.new而是game.new
当一个对象在另一个对象中定义时,它需要一个父对象的实例才能被实例化(它可以从父类实例访问成员信息)。
通常,子类在父类的实例中是“Newed”,因此没有问题,这种模糊,烦人且奇怪的语法正在帮助您在父类之外执行此操作。
此外,你自己也会遇到问题 - 当像“玩家”这样的类没有公共/私人名称使其成为“包私有”时,这意味着它是公共的,它自己的包中的任何东西,私有对所有其他人。