我正在实现一个二十一点游戏,我试图允许多个类(分别为 ChatGUI 和 GameGUI)使用我的“客户端”类,它实际上是服务器的入口。
我尝试使用 Client 类作为每个构造函数的参数来创建这些类,但似乎我的 Client 类无法将信息发送回其他类 - ChatGUI/GameGUI。
我已经有了一个可以处理多个客户端连接的工作服务器!(经过试验和测试)。
public class BlackJack{
private Client client;
private ChatGUI chatgui;
private GameGUI gamegui;
public BlackJack(){
// Setup Client class, which will be passed to all other classes
client = new Client(server, port, username, chatgui, gamegui);
// Setup other classes, which will be given Client class
chatgui = new ChatGUI(client);
gamegui = new GameGUI(client);
}
}
public class Client{
private ChatGUI chatgui;
private GameGUI gamegui;
Client(String server, int port, String username, ChatGUI cg, GameGUI gamegui){
// catch these arguments and assign them to variables
}
void display(String msg){
// Method to display incoming messages to the chat screen
// Using ChatGUI method (*** not working? ***)
chatgui.append(msg + "\n");
}
}
public class ChatGUI{
private JTextArea textarea;
private Client client;
public ChatGUI(Client c){
client = c;
}
// ChatGUI can use client methods
void sendMessage(String msg){
client.sendChat(msg);
}
void append(String msg){
textarea.append(msg);
}
public class GameGUI{
private Client client;
public GameGUI(Client c){
client = c;
}
// GameGUI can use client methods
void playGame(){
client.playGame();
}
}
请注意,此代码是为更多的伪代码参考而编写的 - 不想粘贴数百行。
任何帮助将非常感激!非常感谢您的阅读。