在这个线程中,我发现了一些有趣的时刻,如果类仅用作超类,则没有规则使其抽象。为什么这样?
In this thread I found some interesting moment, If class uses only as superclass there isn't rule to make it abstract. Why so?
谢谢
推荐答案这完全取决于是否< 。
It all depends on whether or not it makes sense to have instances of the class.
假设您有一个 Dog 和一类 Cat 。它们都扩展了 Animal 。现在 Animal 可能有一个名称和一些方法,但是运行 Animal 却没有任何意义。周围。 动物是...很抽象的概念。
Suppose you for instance have one class Dog and one class Cat. They both extend Animal. Now an Animal may have a name and a few methods, but it doesn't make sense to have Animals running around. An Animal is... well an abstract notion.
在其他情况下,您可能会有子类( LinkedHashSet 例如扩展了 HashSet ),但是实例化超类( HashSet )。
In other circumstances you may have subclasses (the LinkedHashSet for instance extends HashSet) but it still makes a lot of sense to instantiate the super class (HashSet in this case).
要回答您的评论, 即使您不想实例化一个类,也可以使其成为非抽象类。
好,我想说的是,今天,只是根本不知道应该实例化它的任何用例,所以应用了相同的规则:拥有该类的实例在逻辑上有意义吗? -abstract。
Well I'd say if you, today, simply don't know of any use-cases in which it should be instantiated, the same rule applies: Does it make sense (logically) to have an instance of the class? If so, make it non-abstract.
如果情况更像是 如果您创建了此类的实例,则可能您做错了! ,然后通过使类抽象化来澄清这一点。
If the situation is more like "If you've created an instance of this class, you're probably doing it wrong!" then I'd clarify this by making the class abstract.
更多推荐
在什么情况下超类不应该是抽象的?
发布评论