即使没有任何抽象方法,类也可以是抽象的吗?如果是有什么用?

发布于 2024-08-19 22:32:34 字数 96 浏览 4 评论 0原文

我怀疑 HttpServlet 类是一个抽象类,尽管该类中没有任何抽象方法,所有方法都是具体的。 即使没有任何抽象方法,类也可以是抽象的吗?如果是有什么用?

谢谢

I have a doubt regarding HttpServlet class is an abstract class even though there is not any abstract method in the class , all methods are concrete.
Can class be abstract even if does not have any abstract methods? If yes Whats the use?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

是伱的 2024-08-26 22:32:34

对于 HttpServlet 来说,关键是 servlet 程序员通常不希望他们的 servlet 支持所有 4 个主要 HTTP 方法(POST、GET、PUT、DELETE),因此这会很烦人使 doGet()doPost() 等方法抽象化,因为程序员将被迫实现他们不需要的方法。因此,HttpServlet 为所有这些方法提供了一个默认实现,除了向客户端返回错误状态代码之外什么也不做。程序员可以重写他们需要的方法,而不必担心其余的。但实际上使用 HttpServlet 类本身没有任何意义(因为它没有做任何有用的事情),所以它是抽象的。

这里有一个很好的例子,说明何时拥有一个没有任何抽象方法的抽象类是有意义的。

In the case of HttpServlet, the point is that servlet programmers typically don't want their servlet to support all 4 oft the main HTTP methods (POST, GET, PUT, DELETE), so it would be annoying to make the doGet(), doPost(), etc. methods abstract, since programmers would be forced to implement methods that they don't need. Therefore, HttpServlet provides a default implementation for all those methods that does nothing except return an error statuscode to the client. Programmers can override the methods they need and not worry about the rest. But actually using the HttpServlet class itself make no sense (since it does nothing useful), so it's abstract.

And there you have a great example for when it can make sense to have an abstract class without any abstract method.

流绪微梦 2024-08-26 22:32:34

无法实例化抽象类,因此您必须对它们进行子类化才能使用它。
在子类中,您仍然可以实现自己的方法或覆盖父方法。

也许独立使用 HttpServlet 没有意义,但它在特定上下文中提供了必要的默认功能。您可以子类化 HttpServlet 来服务此上下文并访问其功能。

HttpServlet 的子类不必实现方法即可使其自身的功能发挥作用。

One cannot instantiate abstract classes so you have to subclass them in order to use it.
In the subclass you can still implement your own methods or override parent methods.

Maybe it doesn't make sense to use HttpServlet as standalone, but it provides necessary default functionality in a certain context. You can subclass HttpServlet to serve this context and to have access to its functionality.

A subclass of HttpServlet doesn't have to implement methods in order to make its own functionality work.

以为你会在 2024-08-26 22:32:34

这是一个意图问题。如果该类是抽象类,则无法创建该类的实例 - 只能创建子类的实例。

我对 Java 不太熟悉,但我的猜测是 HttpServlet 提供了其方法的默认实现,但您需要重写其中一些方法。拥有该类的非专用实例仍然没有什么意义,因此它是抽象的,因此编译器会向任何尝试的人提供提示。

It's a matter of intent. If the class is abstract, you can't create an instance of that class - only of a subclass.

I'm not strong on Java, but my guess is that HttpServlet provides default implementations of it's methods, but that you're expected to override some of them. It still makes little sense to have an unspecialised instance of that class, so it's abstract so the compiler drops a hint to anyone who tries.

在巴黎塔顶看东京樱花 2024-08-26 22:32:34

当您不确定类设计是否完整或者您计划稍后添加一些方法时,即使所有方法都有具体实现,将类标记为抽象也会很有帮助。将已声明为抽象的类更改为以后不再抽象并不会破坏与预先存在的二进制文件的兼容性,而其他方式则会破坏兼容性。

Marking a class as an abstract even when there is a concrete implementation for all methods would be helpful in cases where you are not sure if the class design is complete or there are chances that you plan to add some methods later. Changing a class that has been declared abstract to be made not abstract later doesn't break the compatibility with pre-existing binaries where as other way round breaks compatibility.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文