【问题标题】:How do you pass instances created in main to classes?您如何将 main 中创建的实例传递给类?
【发布时间】:2012-07-16 17:46:08
【问题描述】:

首先,如果这是一个愚蠢的问题,我深表歉意。这最近一直困扰着我,我认为值得一问......

我有一个应用程序,其中的类流动如下:

我目前正试图让我的 Client 类和 Server 类能够访问在 MyApp 的 main 中创建的彼此的实例。问题是实例是在 main 中创建的(因此使它们成为静态的),我想知道如何正确地将它们传递给其他类,因为我所做的似乎不是正确的方法。

这是我所做的:

public class MyApp {
    private static RedClient red_client = null;
    private static BlueClient blue_client = null;
    private static RedServer red_server = null;
    private static BlueServer blue_server = null;

    public static void main(String[] args) {
        final Client myClient = new Client(arg1, arg2);
        red_client = myClient.getRedClient();
        blue_client = myClient.getBlueClient();

        final Server myServer = new Server(arg3, arg4);
        red_server = myServer.getRedServer();
        blue_server = myServer.getBlueServer();
    }

    public static RedClient getRedClient() {
        return red_Client;
    }

    public static BlueClient getBlueClient() {
        return blue_client;
    }

    public static RedServer getRedServer() {
        return red_server;
    }

    public static BlueServer getBlueServer() {
        return blue_server;
    }

}

我稍后会像这样使用以下内容:

public class Client {
    public void SomeMethod {
        MyApp.getBlueServer.doSomething(myObject);
    }
}

我只是不确定这是否是将实例传递给另一个类的正确方法,因为客户端和服务器都与 MyApp 进行通信。 (请忽略类名,因为它们与应用程序的功能无关,我只是将它们用作名称,因为这是我能想到的第一件事)。

如果您需要任何澄清,请告诉我,因为我愿意学习和批评。如果这是错误的方式,请您解释为什么是错误的,然后解释正确的方式。

编辑

进一步说明:

  • MyApp 可以访问客户端和服务器
  • 客户端可以访问 RedClient 和 BlueClient
  • 服务器可以访问 RedServer 和 BlueServer

没有其他类可以相互访问。

【问题讨论】:

    标签: java variables static main


    【解决方案1】:

    我没有看到你传递/设置任何东西。

    没有理由不能将客户端实例传递给服务器(反之亦然):

    Client myClient = new Client(args);
    Server myServer = new Server(args);
    
    BlueClient = myClient.getBlueClient();
    RedCLient = myClient.getRedClient();
    
    BlueServer = myServer.getBlueServer();
    RedServer = myServer.getRedServer();
    
    myClient.addServer(blueServer);
    myClient.addServer(redServer);
    
    myServer.addClient(blueClient);
    myServer.addClient(redClient);
    

    【讨论】:

    • 我很抱歉没有早点澄清这一点。我对原始问题进行了编辑,说明哪些类可以访问哪些类。所以,MyApp 创建了 Client 和 Server,但没有直接访问 RedClient、BlueClient 等的权限。你是建议我添加一些 setter 并直接从 main 设置它?
    • 你没有理由不能这样做。
    • 您甚至可以在Client 中编写一个方法,接收对Server 的引用并添加其所有不同颜色的服务器...您有很多选择。
    • 前几天我脑子里放了个屁。我认为整个“静态”部分让我有点困惑。直到今天才把注意力从这件事上移开,现在我意识到我的问题是多么愚蠢。我只是按照您的建议进行了操作,这也完全摆脱了静态变量(我不认为这是必要的)。谢谢!
    【解决方案2】:
    【解决方案3】:

    我不确定您的要求是什么,但我认为您需要先阅读一些有关面向对象编程的知识。

    对于您刚刚介绍的情况(我不喜欢它,但我认为它更好)您可能有客户端构造函数来接收服务器作为参数:

    Client client1 = new CLient(server1)
    

    因此您将能够从客户端方法访问 server1 对象。

    【讨论】:

    • 如果可能,我不想添加构造函数。不过还是谢谢你的回答
    【解决方案4】:

    您可以使用单例。如果你有一个类并且你可以确定总是最多有一个实例,那么这个实例就是一个单例。我给你举个小例子:

    public class MyClass {
        private static MyClass instance;
        public static MyClass getInstance() {
            if(instance == null)
                instance = new MyClass();
            return instance;
        }
        private MyClass() { }
    }
    

    有了这个,你可以从你代码中的任何地方调用 MyClass.getInstance() 并且你会得到一个单例;想一想;对于您所说的情况,它可能非常实用。

    【讨论】:

    • 谢谢你的例子,我必须阅读单身人士。但是,是的,据我所知,该类始终只有一个实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多