我正在实现一个我一直在努力的网络库,而在使用其中一个服务器系统时遇到了困难。在库中,我有一个名为ConnectedClient的类,该类在远程客户端连接到服务器时创建。换句话说,服务器接收连接时,将创建一个ConnectedClient对象,并将该对象传递给重写的方法。这是相关的服务器代码:public void run() { if (IsConnectionActive()) { System.err.println("Cannot initialize server. Server is already running: " + serverSocket); return; } System.out.println("Initializing multiclient server..."); try { Init(); System.out.println("Server Initialized."); threadActive = true; } catch (ServerInitializeException e) { System.err.println(e.getMessage()); return; } System.out.println("Waiting for client connections..."); while (threadActive) { try { Socket s = serverSocket.accept(); ConnectedClient temp = new ConnectedClient(s); connectedClients.add(temp); System.out.println("Client connection caught and initialized. Client: " + s); System.out.println("Connection with " + s + " now listening for incoming packets."); ThreadAction(temp); CleanClientList(); } catch (SocketTimeoutException e) { } catch (IOException e) { System.err.println("Error accepting a client. Connection refused and reset."); } catch (ConnectionInitializationException e) { System.err.println(e.getMessage() + " Connection refused and reset."); } } } protected void ThreadAction(ConnectedClient client) { }最终用户客户端将覆盖方法ThreadAction,这就是接受新连接的方式。这是ConnectedClient代码:public class ConnectedClient extends Connection implements Runnable { public ConnectedClient(Socket socket) throws ConnectionInitializationException { connectedSocket = socket; OpenIOStreams(); } @Override public void run() { while (IsConnectionActive()) { try { ThreadAction(ReceivePacket()); } catch (ReadPacketException e) { System.err.println(e.getMessage() + " Closing connection."); try { CloseIOStreams(); } catch (ConnectionException e1) { System.err.println(e.getMessage()); } } } } public void ThreadAction(Packet p) { if (p.packetType == Packet.PACKET_TYPE.Message) System.out.println(p.packetString); else if (p.packetType == Packet.PACKET_TYPE.CloseConnection) { System.out.println("Client wishes to close connection. Closing."); try { CloseIOStreams(); } catch (ConnectionException e) { System.err.println(e.getMessage()); } } else { } }}*它扩展的类Connection只是一个包含更多变量的蓝图在实现中,我重写了服务器方法,如下所示:public Server(int port) { super(port); clients = new ArrayList<ClientConnection>(); clientThreads = new ArrayList<Thread>(); } @Override public void ThreadAction(ConnectedClient cc) { // ClientConnection temp = (ClientConnection) cc; // clients.add(temp); Thread t = new Thread(cc); clientThreads.add(t); t.start(); }所有这些都可以,我可以访问服务器中的ConnectedClient对象,发送和接收默认消息等。但是,当我尝试修改ConnectedClient ThreadAction(Packet)方法时,就会出现问题。首先,我尝试在以下位置创建ConnectedClient类的扩展:public class ClientConnection extends ConnectedClient { public ClientConnection(Socket socket) throws ConnectionInitializationException { super(socket); } @Override public void ThreadAction(Packet p) { }}但是,当我尝试在服务器ThreadAction(Packet)方法中为传递的对象分配新对象时(如该方法中的注释所示),抛出了一个异常,说我无法将ConnectedClient转换为ClientConnection。因此,如何覆盖我的ConnectedClient ThreadAction(Packet)方法,并将其与服务器中传递的对象一起使用?例如,如果我希望我的ConnectedClient ThreadAction(Packet)每次客户端向服务器发送一个数据包而不是该消息的消息时都打印出“接收到的数据包”,那么如何在不更改库的情况下覆盖该方法呢?很抱歉,如果我不清楚,或者没有提供足够的信息,我在这个问题上工作了几天已经有点慌张了,这对我有好处。如果需要,我可以提供其他信息。谢谢你的帮助! 最佳答案 但是,当我尝试在服务器ThreadAction(Packet)方法中为传递的对象分配新对象时(如该方法中的注释所示),抛出了一个异常,说我无法将ConnectedClient强制转换为ClientConnection。您不能将ConnectedClient强制转换为ClientConnection。 ClientConnection是ConnectedClient的子类,因此无法转换-如果要转换为父对象,则只能将一个对象转换为另一个对象。也就是说,您只能将ClientConnection强制转换为ConnectedClient。考虑类java.lang.String和java.lang.Object。您可以将String强制转换为Object,因为String是对象的子类,因此,Object不能将Object强制转换为String,因为无法验证确实是Object。这就是为什么您不能投射对象的原因。您要做的就是创建一个可以将两者转换的方法。此方法应包含一个String,并返回一个ConnectedClient。public static ClientConnection conntectedClientToClientConnection(ConnectedClient client){ //Make a ClientConnection that represents the ConnectionClient object //Return it}您也可以在ClientConnection中创建一个接受ClientConnection的构造函数。在方法/构造函数内部,您将需要“复制变量”。也就是说,请确保ConnectionClient对象内的所有变量与ClientConnection对象内的变量相同。您可以尝试的另一件事是更改服务器代码类中的代码。更改此:Socket s = serverSocket.accept();ConnectedClient temp = new ConnectedClient(s);connectedClients.add(temp);System.out.println("Client connection caught and initialized. Client: " + s);System.out.println("Connection with " + s + " now listening for incoming packets.");ThreadAction(temp);CleanClientList();对此:Socket s = serverSocket.accept();ClientConnection temp = new ClientConnection(s); //Changed this lineconnectedClients.add(temp);System.out.println("Client connection caught and initialized. Client: " + s);System.out.println("Connection with " + s + " now listening for incoming packets.");ThreadAction(temp);CleanClientList();只要ConnectionClient类还具有一个接受套接字的构造函数,这将满足您的要求。 08-27 14:48