受保护方法允许的访问修饰符是什么
What are the access modifiers allowed for protected methods
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SampleServlet
*/
@WebServlet("/SampleServlet")
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SampleServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);``
}
}
在 eclipse 上默认生成的 servlet 代码中,我发现 doGet 和 doPost 方法具有受保护的访问权限。
- doXXX 方法有哪些不同的访问修饰符?
- 它将如何影响 class 的行为?
您不能对继承方法的访问进行更严格的限制(有关更多详细信息,您可以查看 oracle documentation)。
它不会影响 servlet 的行为,因为它只是从其超级 class 服务方法(请参阅 source code)调用的分派方法。
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SampleServlet
*/
@WebServlet("/SampleServlet")
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SampleServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);``
}
}
在 eclipse 上默认生成的 servlet 代码中,我发现 doGet 和 doPost 方法具有受保护的访问权限。
- doXXX 方法有哪些不同的访问修饰符?
- 它将如何影响 class 的行为?
您不能对继承方法的访问进行更严格的限制(有关更多详细信息,您可以查看 oracle documentation)。
它不会影响 servlet 的行为,因为它只是从其超级 class 服务方法(请参阅 source code)调用的分派方法。