java - 在多个类之间共享对象

标签 java class object

我正在实现一个 Blackjack 游戏,我正在尝试允许多个类(分别是 ChatGUI 和 GameGUI)使用我的“Client”类,它实际上是服务器的入口。

我已经尝试使用 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();
    }
}

请注意,此代码是为更多的伪代码引用而编写的 - 不想粘贴数百行。

任何帮助将不胜感激!非常感谢阅读。

最佳答案

假设我正确地理解了您的问题,我认为以稍微不同的方式构建您的类对您是有益的...

每个 BlackJack 对象都有一个客户端,例如

public class BlackJack{
    private Client client;

    public BlackJack(){
        // Setup Client class, which will be passed to all other classes
        client = new Client(server, port, username);

    }  

然后每个客户端对象都有一个 GUI 类的实例,例如

public class Client{
    private ChatGUI chatgui;
    private GameGUI gamegui;

    Client(String server, int port, String username){
             chatgui = new ChatGUI(this);
             gamegui = new GameGUI(this);
    }

    //handle messages from server
    void onMessageRecieved(String msg){
            if(/* the message pertains to the game gui */ )
                    gamegui.newMessage(msg);
            else if( /* the message pertains to the chat gui */ )
                    chatgui.newMessage(msg);
    }

    //here you can add functions for the gui classes to call
    public void sendChat(String chat){

    }
}

那么你的 gui 类可能看起来像...

public class ChatGUI{
    private JTextArea textarea;
    private Client client;

    public ChatGUI(Client c){
        client = c;
    }

    //receive message/command from server
    public void newMessage(String msg){
        //perform the desired action based on the command
    }

    public void sendChat(String msg){
        client.sendChat(msg);
    }
}

public class GameGUI{
    private Client client;

    public GameGUI(Client c){
        client = c;
    }

    //receive message/command from server
    public void newMessage(String msg){
        //perform the desired action based on the command
    }
}

关于java - 在多个类之间共享对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16196194/

相关文章:

python - 你能将一个对象 append 到Python中已经实例化的对象吗?

c# - 如何重用相似但不同的类属性

r - 如何在 R 中创建虚拟引用类?

java - Alfresco: checkin 在之前某个时间点 checkout 的文档(并且悬而未决)

java - doThrow 异常不抛出 Spring MVC 单元测试?

python - 为什么有些库只编写私有(private)类函数,然后分配一个公共(public)变量来公开它们?

Objective-C id 和 NSObject

java - 我可以聚焦php来在php中键入变量类吗?

java - 修剪字节数组末尾的空元素 - Java

java - servlet 的 Hibernate 超时问题