在什么情况下超类不应该是抽象的?

编程入门 行业动态 更新时间:2024-10-12 05:50:17
本文介绍了在什么情况下超类不应该是抽象的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

在什么情况下超类不应该是抽象的?

本文发布于:2023-11-03 21:33:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不应   抽象   情况下

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!