在什么情况下超类不应该是抽象的? [英] In what cases superclass shouldn't be abstract?

查看:117
本文介绍了在什么情况下超类不应该是抽象的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个线程中,我发现了一些有趣的时刻,如果类仅用作超类,则没有规则使其抽象。为什么这样?

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.

这篇关于在什么情况下超类不应该是抽象的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆