我實現一個21點遊戲,我試圖讓多個類 - (分別ChatGUI和GameGUI)用我的「客戶」類,這是有效的門口到服務器。共享對象
我試着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();
}
}
請注意,此代碼是針對更多僞代碼引用編寫的 - 不想粘貼數百行代碼。
任何幫助將不勝感激!非常感謝您的閱讀。
從服務器類/方法單獨的線程上運行的客戶端類/方法?如果沒有,那麼你的服務器將不會從客戶端得到任何數據,而不明確地調用它們 – 2013-04-24 15:38:57