java 为什么要使用没有抽象方法的抽象类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6856133/
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 use an abstract class without abstract methods?
提问by hongtaesuk
I am now studying a java and I'm at the part of Abstract. I read sorta strange part to me that there is an abstract class which does not include any abstarct method.
我现在正在学习 Java 并且我在 Abstract 的一部分。我读到有点奇怪,有一个抽象类不包含任何抽象方法。
Why do they use this kind of class?
他们为什么要使用这种类?
回答by Bozho
To prevent instantiation of that class and use it only as a base class. Child classes can use the general methods defined in the abstract class.
防止实例化该类并仅将其用作基类。子类可以使用抽象类中定义的通用方法。
For example it doesn't make sense to create an instance of AbstractVehicle
. But All vehicles can reuse a common registerMileage(int)
method.
例如,创建AbstractVehicle
. 但所有车辆都可以重用通用registerMileage(int)
方法。
回答by Bohemian
A common reason to do this is to have the abstract
class provide exploding implementations of the abstract
methods as a convenience to subclasses who don't have to implement allthe abstract
methods, just those they want to - the remaining ones will still explode but it won't matter if those execution paths aren't exercised.
这样做的一个常见原因是让abstract
类提供abstract
方法的爆炸式实现,以方便子类不必实现所有abstract
方法,只需实现他们想要的方法 - 其余的仍然会爆炸,但不会如果不执行这些执行路径,则无关紧要。
HttpServletis an example of this pattern in action. It has default implementations for all methods that handle the different request types, but they all throw an exception. The subclass must override these if they want to do something meaningful. It's OK to leave some handler methods not overridden as long as they are never called.
HttpServlet是这种模式的一个例子。它具有处理不同请求类型的所有方法的默认实现,但它们都抛出异常。如果子类想要做一些有意义的事情,他们必须覆盖这些。只要从不调用某些处理程序方法,就可以不覆盖它们。
回答by Maulik Kakadiya
Yes, we can have abstract class without any abstract method. Best example of abstract class without any abstract method is HttpServlet
是的,我们可以拥有没有任何抽象方法的抽象类。 没有任何抽象方法的抽象类的最佳示例是 HttpServlet
回答by Jigar Joshi
If this class extends another abstract class and don't have implementation of inherited abstract methods.
如果这个类扩展了另一个抽象类并且没有继承抽象方法的实现。
回答by phoenix.ru.smr
This class contains some common logic for all its inheritors, but itself does not represent usable entity (in terms of particular application)
此类包含其所有继承者的一些通用逻辑,但本身并不代表可用实体(就特定应用而言)
回答by Kumar Manish
Say you have a set of related classes, but no related (shared) code, yet. If we make all of these classes extend a base class with no abstract methods, that then if we wan't all of these classes to have an identical method/feature in the future, that can be done in one shot by putting it in the base class. So code is not repeated and it reflects in all child classes by including it in just one place.
假设你有一组相关的类,但没有相关的(共享)的代码,但。如果我们让所有这些类都扩展一个没有抽象方法的基类,那么如果我们将来不想所有这些类都具有相同的方法/功能,可以通过将其放入基类。所以代码不会重复,它通过将它包含在一个地方来反映在所有子类中。
回答by Sumit
These type of classes are used for a implement a general logic which can be implemented by other classes. Making it abstract prevents from instantiating it. But other classes can inherit the class and its methods.
这些类型的类用于实现可由其他类实现的通用逻辑。使其抽象可防止实例化它。但是其他类可以继承该类及其方法。
回答by NiVeR
Another example for having such class is when you implement creation helpers. These classes are used to ease the client in the creation of objects, which are related in topic but decoupled depending on the need. By nature, the methods of this creator classes are all static and they can be seen as utility classes as well.Obviously, instatntation of this classes is futile and hence the abstract
keyword.
拥有此类的另一个示例是当您实现创建助手时。这些类用于简化客户端创建对象的过程,这些对象在主题上是相关的,但根据需要进行解耦。本质上,这个创建者类的方法都是静态的,它们也可以看作是实用类。显然,这个类的实例化是徒劳的,因此是abstract
关键字。
To mention a recent example I met was the Sftp
class from org.springframework.integration.dsl.sftp
which is basically an easy way to require objects (e.g: adapters, gateways) from the sftp api.
提一下我最近遇到的一个例子,这个Sftp
类org.springframework.integration.dsl.sftp
基本上是一种从 sftp api 要求对象(例如:适配器、网关)的简单方法。
回答by user3359139
I develop a abstract class to prevent instantiation of that class and use it only as a base class. because, These type of classes are used for a implement a general logic which can be implemented by other classes. Sometimes, I have a default implementation for every method in abstract class. In the manner, it doesn't force the sub-class to override all of method, but also it implement everyone that is need.It means implicitly you have to override at least one method to make scene using this abstract class.
我开发了一个抽象类来防止该类的实例化,并且仅将其用作基类。因为,这些类型的类用于实现可由其他类实现的通用逻辑。有时,我对抽象类中的每个方法都有一个默认实现。以这种方式,它不强制子类覆盖所有方法,而且它实现了每个需要的人。这意味着你必须隐式地覆盖至少一个方法来使用这个抽象类来制作场景。
回答by Bart van Heukelom
I can't think of any good reason to use it. It could be used as "marker" but an interface would be a better choice.
我想不出任何使用它的好理由。它可以用作“标记”,但界面将是更好的选择。