本文介绍了抽象类应该至少有一个抽象方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
抽象类是否有必要至少有一个抽象方法?
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的答案是主观的,是风格问题。我个人会说是的。如果你的目的是阻止一个类(没有抽象方法)被实例化,处理这个的最好方法是使用 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 protected
constructor, not by marking it abstract
.
这篇关于抽象类应该至少有一个抽象方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!