将子 class 对象分配给父 class 的对象
Assigning a child class object to an object of the parent class
我正在实现一个我一直在努力的网络库,但我在使用其中一个服务器系统时遇到了困难。
在库中,我有一个名为 ConnectedClient
的 class,它是在远程客户端连接到服务器时创建的。换句话说,当服务器接收到连接时,它会创建一个 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 {
}
}
}
*它扩展出来的classConnection
只是一个包含更多变量的蓝图
在实现中,我覆盖了服务器方法,如下所示:
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
class 的扩展,这里:
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)
每次客户端向服务器发送数据包时打印出 "packet received",而不是数据包的消息,我如何重写方法来执行此操作,而不更改图书馆?
抱歉,如果我不清楚,或者没有提供足够的信息,我有点慌乱,因为我已经在这个问题上工作了几天,现在它让我变得更好了。如果需要,我可以提供更多信息。
感谢您的帮助!
为什么不更改以下 server
代码:
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);
至:
ClientConnection temp = new ClientConnection(s); // only this is changed
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);
我认为您不会得到: cannot cast ConnectedClient to ClientConnection
更改后。您可以用自己的方式覆盖 ClientConnection.ThreadAction
。请告诉我它是否有效。
However, when I tried to assign a new object to the passed object in my server ThreadAction(Packet) method (as shown in my comments within that method), an exception was thrown saying that I cannot cast ConnectedClient to ClientConnection.
你不能将ConnectedClient
转换为ClientConnection
。 ClientConnection
是 ConnectedClient
的子对象 class,因此无法进行转换 - 如果要转换为父对象,则只能将一个对象转换为另一个对象。也就是说,您只能将 ClientConnection
转换为 ConnectedClient
.
想想 classes java.lang.String
和 java.lang.Object
。您可以将 String
转换为 Object
,因为 String
是对象的子对象 class,因此是 Object
,您不能将 Object
到 String
因为无法验证 Object
是否确实是 String
。这就是为什么你不能投射你的对象。
您要做的是创建一个可以将两者转换的方法。这个方法应该接受一个ConnectedClient
和return一个ClientConnection
.
public static ClientConnection conntectedClientToClientConnection(ConnectedClient client){
//Make a ClientConnection that represents the ConnectionClient object
//Return it
}
您还可以在 ClientConnection
中创建一个接受 ConnectionClient
.
的构造函数
在 method/constructor 内,您将需要 "copy the variables"。也就是说,确保 ClientConnection
对象中的所有变量与 ConnectionClient
对象中的变量相同。
您可以尝试的另一件事是更改服务器代码中的代码 class。
改变这个:
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 line
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();
如果 ClientConnection
class 也有一个接受套接字的构造函数,这将满足您的要求。
我正在实现一个我一直在努力的网络库,但我在使用其中一个服务器系统时遇到了困难。
在库中,我有一个名为 ConnectedClient
的 class,它是在远程客户端连接到服务器时创建的。换句话说,当服务器接收到连接时,它会创建一个 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 {
}
}
}
*它扩展出来的classConnection
只是一个包含更多变量的蓝图
在实现中,我覆盖了服务器方法,如下所示:
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
class 的扩展,这里:
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)
每次客户端向服务器发送数据包时打印出 "packet received",而不是数据包的消息,我如何重写方法来执行此操作,而不更改图书馆?
抱歉,如果我不清楚,或者没有提供足够的信息,我有点慌乱,因为我已经在这个问题上工作了几天,现在它让我变得更好了。如果需要,我可以提供更多信息。
感谢您的帮助!
为什么不更改以下 server
代码:
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);
至:
ClientConnection temp = new ClientConnection(s); // only this is changed
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);
我认为您不会得到: cannot cast ConnectedClient to ClientConnection
更改后。您可以用自己的方式覆盖 ClientConnection.ThreadAction
。请告诉我它是否有效。
However, when I tried to assign a new object to the passed object in my server ThreadAction(Packet) method (as shown in my comments within that method), an exception was thrown saying that I cannot cast ConnectedClient to ClientConnection.
你不能将ConnectedClient
转换为ClientConnection
。 ClientConnection
是 ConnectedClient
的子对象 class,因此无法进行转换 - 如果要转换为父对象,则只能将一个对象转换为另一个对象。也就是说,您只能将 ClientConnection
转换为 ConnectedClient
.
想想 classes java.lang.String
和 java.lang.Object
。您可以将 String
转换为 Object
,因为 String
是对象的子对象 class,因此是 Object
,您不能将 Object
到 String
因为无法验证 Object
是否确实是 String
。这就是为什么你不能投射你的对象。
您要做的是创建一个可以将两者转换的方法。这个方法应该接受一个ConnectedClient
和return一个ClientConnection
.
public static ClientConnection conntectedClientToClientConnection(ConnectedClient client){
//Make a ClientConnection that represents the ConnectionClient object
//Return it
}
您还可以在 ClientConnection
中创建一个接受 ConnectionClient
.
在 method/constructor 内,您将需要 "copy the variables"。也就是说,确保 ClientConnection
对象中的所有变量与 ConnectionClient
对象中的变量相同。
您可以尝试的另一件事是更改服务器代码中的代码 class。
改变这个:
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 line
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();
如果 ClientConnection
class 也有一个接受套接字的构造函数,这将满足您的要求。