Java 未调用基于接口的多态性
Java Interface-Based Polymorphism Not Being Called
我正在开发一个程序,该程序将命令作为字符串数据接收并将其传递给正确的处理程序,如 HashMap
所定义。
将命令传递给正确处理程序的代码如下:
//This is run in its own thread, hence the run() method
@Override
public void run() {
try {
//Socket is a Socket object containing the client's connection
InputStreamReader is = new InputStreamReader(socket.getInputStream());
//MAX_MESSAGE_SIZE is an integer, specifically 1024
char[] data = new char[MAX_MESSAGE_SIZE];
is.read(data, 0, MAX_MESSAGE_SIZE);
String strData = new String(data);
//Split on UNIX or Windows type newline
String[] lines = strData.split("\r?\n");
//First verb determines the command
String command = (lines[0].split(": "))[0];
//Re-add the ": " because the HashMap will look for it
if (handlerMap.containsKey((command + ": "))) {
System.err.println("Got command: " + command);
AbstractHandler handler = handlerMap.get(command);
System.err.println("Passing connection + data to handler...");
handler.handleData(socket, lines);
System.err.println("Handler is done!");
} else {
System.err.println("Didn't understand command: " + command);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes(Protocol.UNKNOWN_ERROR);
outputStream.flush();
socket.close();
}
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
}
HashMap
的值部分是一个实现接口 AbstractHandler
的对象。 AbstractHandler
定义了一种方法: handleData(Socket s, String[] lines)
。作为参考,这里是地图初始化的地方:
public RequestManager(Socket socket) {
this.socket = socket;
handlerMap = new HashMap<>();
handlerMap.put(Protocol.USERNAME, new AuthenticationHandler());
//Set arg to true b/c it is a set request, not a change request
handlerMap.put(Protocol.SET_PASSWORD, new ChangePasswordHandler(true));
handlerMap.put(Protocol.CHANGE_PASSWORD, new ChangePasswordHandler());
handlerMap.put(Protocol.FORGOT_PASSWORD, new ForgotPasswordHandler());
}
对象中的所有handleData
方法只包含以下代码:
@Override
public void handleData(Socket s, String[] lines) {
clientSocket = s; //clientSocket field in class
System.err.println("Made it into handler");
}
奇怪的是在显示"Passing connection + data to handler"之后,没有任何反应。我没有看到任何有关进入处理程序的信息,也没有看到异常或处理程序已完成的消息。这可能是什么原因造成的?
你用
测试一下处理程序是否存在
if (handlerMap.containsKey((command + ": "))) {
但你尝试用
获得一个处理程序
AbstractHandler handler = handlerMap.get(command);
因此,如果密钥 CommandName:
存在,您可能无法使用密钥 CommandName
获取它。因此,您将在调用 handler.handleData(socket, lines);
时有一个未经检查的 NullPointerException
,并且您的可运行程序将死得很惨。
看来您需要更改第一个或第二个。鉴于您说您会看到它打印 "Passing connection + data to handler..." 我认为您需要将其更改为:
AbstractHandler handler = handlerMap.get(command + ": ");
当你处理地图时,在风格上做一个小的改变就可以防止现在和将来出现这种情况。 Map.get
returns null
if the key is not found,所以你可以这样做:
AbstractHandler handler = handlerMap.get(command + ": ");
if (handler != null) {
/* ... */
handler.handleData(socket, lines);
System.err.println("Handler is done!");
} else {
/* ... */
}
我正在开发一个程序,该程序将命令作为字符串数据接收并将其传递给正确的处理程序,如 HashMap
所定义。
将命令传递给正确处理程序的代码如下:
//This is run in its own thread, hence the run() method
@Override
public void run() {
try {
//Socket is a Socket object containing the client's connection
InputStreamReader is = new InputStreamReader(socket.getInputStream());
//MAX_MESSAGE_SIZE is an integer, specifically 1024
char[] data = new char[MAX_MESSAGE_SIZE];
is.read(data, 0, MAX_MESSAGE_SIZE);
String strData = new String(data);
//Split on UNIX or Windows type newline
String[] lines = strData.split("\r?\n");
//First verb determines the command
String command = (lines[0].split(": "))[0];
//Re-add the ": " because the HashMap will look for it
if (handlerMap.containsKey((command + ": "))) {
System.err.println("Got command: " + command);
AbstractHandler handler = handlerMap.get(command);
System.err.println("Passing connection + data to handler...");
handler.handleData(socket, lines);
System.err.println("Handler is done!");
} else {
System.err.println("Didn't understand command: " + command);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes(Protocol.UNKNOWN_ERROR);
outputStream.flush();
socket.close();
}
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
}
HashMap
的值部分是一个实现接口 AbstractHandler
的对象。 AbstractHandler
定义了一种方法: handleData(Socket s, String[] lines)
。作为参考,这里是地图初始化的地方:
public RequestManager(Socket socket) {
this.socket = socket;
handlerMap = new HashMap<>();
handlerMap.put(Protocol.USERNAME, new AuthenticationHandler());
//Set arg to true b/c it is a set request, not a change request
handlerMap.put(Protocol.SET_PASSWORD, new ChangePasswordHandler(true));
handlerMap.put(Protocol.CHANGE_PASSWORD, new ChangePasswordHandler());
handlerMap.put(Protocol.FORGOT_PASSWORD, new ForgotPasswordHandler());
}
对象中的所有handleData
方法只包含以下代码:
@Override
public void handleData(Socket s, String[] lines) {
clientSocket = s; //clientSocket field in class
System.err.println("Made it into handler");
}
奇怪的是在显示"Passing connection + data to handler"之后,没有任何反应。我没有看到任何有关进入处理程序的信息,也没有看到异常或处理程序已完成的消息。这可能是什么原因造成的?
你用
测试一下处理程序是否存在if (handlerMap.containsKey((command + ": "))) {
但你尝试用
获得一个处理程序AbstractHandler handler = handlerMap.get(command);
因此,如果密钥 CommandName:
存在,您可能无法使用密钥 CommandName
获取它。因此,您将在调用 handler.handleData(socket, lines);
时有一个未经检查的 NullPointerException
,并且您的可运行程序将死得很惨。
看来您需要更改第一个或第二个。鉴于您说您会看到它打印 "Passing connection + data to handler..." 我认为您需要将其更改为:
AbstractHandler handler = handlerMap.get(command + ": ");
当你处理地图时,在风格上做一个小的改变就可以防止现在和将来出现这种情况。 Map.get
returns null
if the key is not found,所以你可以这样做:
AbstractHandler handler = handlerMap.get(command + ": ");
if (handler != null) {
/* ... */
handler.handleData(socket, lines);
System.err.println("Handler is done!");
} else {
/* ... */
}