Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在多个类之间共享对象_Java_Class_Object - Fatal编程技术网

Java 在多个类之间共享对象

Java 在多个类之间共享对象,java,class,object,Java,Class,Object,我正在实现一个21点游戏,我正在尝试允许多个类(分别是ChatGUI和GameGUI)使用我的“Client”类,它实际上是服务器的入口 我已经尝试过用客户机类作为每个构造函数的参数来创建这些类,但似乎我的客户机类无法将信息发送回其他类——ChatGUI/GameGUI 我已经有了一个可以处理多个客户端连接的工作服务器!(经过考验) 请注意,这段代码是为更多的伪代码引用而编写的-不想粘贴数百行 任何帮助都将不胜感激!非常感谢您的阅读。嗯,我想知道我是否理解正确,但您想要的是:客户端1向客户端2发

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

我已经尝试过用客户机类作为每个构造函数的参数来创建这些类,但似乎我的客户机类无法将信息发送回其他类——ChatGUI/GameGUI

我已经有了一个可以处理多个客户端连接的工作服务器!(经过考验)

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


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

嗯,我想知道我是否理解正确,但您想要的是:客户端1向客户端2发送聊天信息吗

换句话说:chat1-->客户端1-->客户端2-->GUI 2


如果这是正确的,那么消息是如何传输到client2的呢?

嗯,我想知道我是否理解正确,但您想要的是:Client1向client2发送聊天消息吗

换句话说:chat1-->客户端1-->客户端2-->GUI 2


如果这是正确的,那么消息是如何传输到client2的?

您正在将chatGUI的引用传递到客户端类,然后在这样做之后,实际实例化chatGUI。所以前面传递的引用是不正确的。您需要首先实例化chatGUI和gameGUI对象。然后创建客户机对象并正确设置引用。不要试图在构造函数中传递引用,因为此时它们是无效的。

您正在向客户端类传递chatGUI引用,然后在这样做之后,实际实例化chatGUI。所以前面传递的引用是不正确的。您需要首先实例化chatGUI和gameGUI对象。然后创建客户机对象并正确设置引用。不要试图在构造函数中传递引用,因为在那个时候它们是无效的。

假设我正确理解了您的问题,我认为稍微改变一下类的结构对您是有益的

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

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

假设我正确地理解了你的问题,我认为让你的课程结构有点不同将是有益的

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

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

您的客户机类/方法是否在与服务器类/方法不同的线程上运行?如果没有,那么您的服务器在没有显式调用客户机的情况下将无法从客户机获取任何数据。您的客户机类/方法是否在与服务器类/方法不同的线程上运行?如果不是,则服务器在未显式调用客户端之前将无法从客户端获取任何数据。此问题应为注释。此问题应为注释。