java 为什么 HttpServlet 类被声明为抽象类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6742533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why HttpServlet class is declared as abstract?
提问by satheesh
Why HttpServlet
class is declared as abstract even there is no abstract method in that class?
为什么HttpServlet
类被声明为抽象类,即使该类中没有抽象方法?
回答by BalusC
It's because it follows the Template Methoddesign pattern. The doXxx()
methods have all default behaviours of returning a HTTP 405 Method Not Implementederror. If those methods were all abstract, you would be forced to override them all, even though your business requirements don't need it at all. It would only result in boilerplate code and unspecified/unintuitive behaviour.
这是因为它遵循模板方法设计模式。这些doXxx()
方法具有返回 HTTP 405方法未实现错误的所有默认行为。如果这些方法都是抽象的,您将被迫全部覆盖它们,即使您的业务需求根本不需要它。它只会导致样板代码和未指定/不直观的行为。
回答by Sanjay T. Sharma
It is declared as a class instead of an interface to implement most of the cruft/repeatable code required for setting up a servlet. It is declared as abstract
since it wouldn't make sense to instantiate a "bare bones" servlet which takes care only of the setup and doesn't contain any custom/user defined logic in it.
它被声明为一个类而不是一个接口,以实现设置 servlet 所需的大部分 cruft/repeatable 代码。它被声明为abstract
因为实例化一个“裸机”servlet 是没有意义的,它只负责设置并且其中不包含任何自定义/用户定义的逻辑。
回答by Viral Jain
Probably, to prevent direct instantiation of HttpServlet by creating its instance.
可能是为了防止通过创建其实例来直接实例化 HttpServlet。
In other words, to make sure that whenever HttpServlet is used, it is always extended by the subclass.
换句话说,要确保每当使用 HttpServlet 时,它总是由子类扩展。
No... It can be overridden. Here is the example from Herbert Shcildt Java 2: The Complete reference
不...它可以被覆盖。这是 Herbert Shcildt Java 2 中的示例:完整参考
import java.io.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.*;
public class HelloServlet extends GenericServlet
public class HelloServlet extends GenericServlet
{
{
public void service(ServletRequest request,ServletResponse response) throws ServletException, IOException
public void service(ServletRequest request,ServletResponse response) throws ServletException, IOException
{
{
response.setContentType("text/html");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
PrintWriter pw = response.getWriter();
pw.println("<B>Hello!");
pw.println("<B>Hello!");
pw.close();
pw.close();
}
}
}
}
回答by anup verma
all the methods of HttpServlet class are concrete even we declare this class as abstract because 1>any class can have null or more abstract method as well as concrete method . 2>always we make our servlet by extending HttpServlet.hence only derived servlet class can make object.
HttpServlet 类的所有方法都是具体的,即使我们将这个类声明为抽象类,因为 1> 任何类都可以具有 null 或更多抽象方法以及具体方法。2>我们总是通过扩展HttpServlet来制作我们的servlet。因此只有派生的servlet类才能制作对象。
回答by Priyanka Vishwakarma
HttpServlet is an abstract class without any abstract method because the web container manufacturer restricts developer to create an object of it by declaring it abstract.
HttpServlet 是一个没有任何抽象方法的抽象类,因为 Web 容器制造商通过声明抽象来限制开发人员创建它的对象。
Web container should call doGet() and doPost() and it should handle all the life cycle of Servlet.
Web 容器应该调用 doGet() 和 doPost() 并且它应该处理 Servlet 的所有生命周期。
If we are allowed to create an object of HttpServlet, we are restricting container to call doGet() and doPost(), but that is the job of container itself, it should handle all the life cycle of Servlet.
如果允许我们创建HttpServlet 的对象,我们限制容器调用doGet() 和doPost(),但那是容器本身的工作,它应该处理Servlet 的所有生命周期。
So, that is why, we should not create an object of HttpServlet, so that is why it is marked abstract class without it having any abstract method.
所以,这就是为什么我们不应该创建 HttpServlet 的对象,这就是为什么它被标记为抽象类而没有任何抽象方法。
回答by anilparitala
HttpServlet class is declared as abstract class why because sun people dont want to create an object to HttpServlet class for this reason it is declared as abstract.
HttpServlet 类被声明为抽象类,为什么因为太阳人们不想为 HttpServlet 类创建对象,因此将其声明为抽象类。
How you know that HttpServlet class have no abstarct methods
你怎么知道 HttpServlet 类没有抽象方法
public class TestServlet extends HttpServlet{ } //compile the above program if we are able to compile then there are no abstract methods in the HttpServlet class which we need to over ride.
public class TestServlet extends HttpServlet{ } //编译上面的程序,如果我们能够编译,那么HttpServlet类中就没有我们需要重写的抽象方法。