关于接口的疑问。
接口文件:
public interface HTMLAction extends Serializable {
public void setServletContext(ServletContext context);
public void doStart(HttpServletRequest request);
public Event perform(HttpServletRequest request) throws HTMLActionException;
public void doEnd(HttpServletRequest request, EventResponse eventResponse);
}
例子程序:
private HTMLAction getAction(URLMapping urlMapping) {
HTMLAction handler = null;
String actionClassString = null;
if (urlMapping != null) {
actionClassString = urlMapping.getWebAction();
if (urlMapping.isAction()) {
try {
handler = (HTMLAction) getClass().getClassLoader().loadClass(
actionClassString).newInstance();
}
catch (Exception ex) {
System.err.println("RequestProcessor caught loading action: " + ex);
}
}
}
return handler;
}
片段2:
HTMLAction action = getAction(urlMapping);
if (action != null) {
action.setServletContext(request.getSession().getServletContext());
action.doStart(request);
ev = action.perform(request);
EventResponse eventResponse = null;
1)为什么一个函数的返回值可以是接口?
private HTMLAction getAction(URLMapping urlMapping) { }
2) 为什么接口能定义变量?
HTMLAction handler = null;
3)一个类实例为什么能被强制转成接口?
HTMLAction handler = (HTMLAction) getClass().getClassLoader().loadClass(
actionClassString).newInstance();
4)为什么接口自己调用了起来?
action.setServletContext(request.getSession().getServletContext());
action.doStart(request);