抽象类应该至少有一个抽象方法吗?
问题描述:
抽象类是否有必要至少有一个抽象方法?
Is it necessary for an abstract class to have at least one abstract method?
答
这篇文章的主题和身体问两个不同的问题:
The subject of this post and the body ask two different questions:
- 它应该至少有一个抽象成员吗?
- 是吗?至少有一个抽象成员需要吗?
#2的答案绝对没有。
#1的答案是主观的,是风格问题。我个人会说是的。如果你的目的是阻止一个类(没有抽象方法)被实例化,处理这个的最好方法是使用 private
protected
构造函数,而不是标记 abstract
。
The answer to #1 is subjective and a matter of style. Personally I would say yes. If your intent is to prevent a class (with no abstract methods) from being instantiated, the best way to handle this is with a private
protected
constructor, not by marking it abstract
.