我已经阅读了有关non-static variable this cannot be referenced from a static context
错误的信息,但我不明白为什么在我的情况下得到它(行return new CommandParser1(command);
)?我只是创建类的实例。就这样。问题是什么?
public class ProtocolUtility {
public static CommandParser createParser(String command) throws Exception {
switch (command) {
case COMMAND_1:
return new CommandParser1(command);
case COMMAND_2:
return new CommandParser2(command);
default:
return null;
}
}
public abstract class CommandParser {
protected String command;
public String getCommand() {
return command;
}
}
public class CommandParser1 extends CommandParser {
public CommandParser1 (String command){
//...
}
}
public class CommandParser2 extends CommandParser {
public CommandParser2 (String command) {
//...
}
}
}