黑马程序员技术交流社区
标题:
关于子类继承父类问题.
[打印本页]
作者:
qowoeo7538
时间:
2015-1-1 00:47
标题:
关于子类继承父类问题.
有一个抽象MainServlet类,继承了HttpServlet。又写了一个普通类AdminServlet,继承了MainServlet。当请求指向AdminServlet时,又走了MainServlet中的post方法,很是不解啊。。。另外,在post方法中打印this,竟然是AdminServlet的hash值。为什么啊???
这是代码,
MainServlet :
public abstract class MainServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String methodName = req.getParameter("cmd");
if(methodName==null || methodName.trim().equals("")){
methodName="execute";
}
try {
Method method = this.getClass().getMethod(methodName,
HttpServletRequest.class,HttpServletResponse.class);
method.invoke(this,req, resp);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(),e);
}
}
public abstract void execute(HttpServletRequest req,HttpServletResponse resp)
throws Exception;
}
AdminServlet :
public class AdminServlet extends MainServlet {
private AdminService service = new AdminService();
public void execute(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
String name = req.getParameter("name");
String password = req.getParameter("password");
Admin admin = null;
if(admin==null){
System.err.println("wrong");
}
}
作者:
斷灬dian
时间:
2015-1-1 10:35
元旦快乐!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2