从Vector继承的Java类Stack的负面影响是什么? [英] What are the negative aspects of Java class Stack inheriting from Vector?

查看:356
本文介绍了从Vector继承的Java类Stack的负面影响是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过扩展类Vector,Java的设计者能够快速创建类Stack。什么是
这种继承使用的消极方面,特别是对于类Stack?

By extending class Vector, Java’s designers were able to create class Stack quickly. What are the negative aspects of this use of inheritance, particularly for class Stack?

非常感谢。

推荐答案

一个问题是Stack是一个类,而不是一个接口。这与集合框架的设计不同,其中您的名词通常表示为接口(例如,List,Tree,Set等),并且存在特定实现(例如,ArrayList,LinkedList)。如果Java可以避免向后兼容,那么更合适的设计就是拥有一个Stack接口,然后将VectorStack作为一个实现。

One problem is that Stack is a class, not an interface. This diverges from the design of the collection framework, where your noun is typically represented as an interface (e.g., List, Tree, Set, etc.), and there are specific implementations (e.g., ArrayList, LinkedList). If Java could avoid backward compatibility, then a more proper design would be to have a Stack interface, then VectorStack as an implementation.

第二个问题是Stack现在被绑定了to Vector,通常避免使用ArrayLists等。

A second problem is that Stack is now bound to Vector, which is generally avoided in favour of ArrayLists and the like.

第三个问题是你不能轻易提供自己的堆栈实现,而且堆栈支持非常-stack操作,例如从特定索引获取元素,包括索引异常的可能性。作为用户,您可能还必须知道堆栈的顶部是在索引0还是在索引n处。该接口还公开了容量等实现细节。

A third problem is that you cannot easily provide your own stack implementation, and that stacks support very non-stack operations like getting an element from a specific index, including the potential for index exceptions. As a user, you may also have to know if the top of the stack is at index 0 or at index n. The interface also exposes implementation details such as capacity.

在原始Java类库中的所有决策中,我认为这是一个比较奇特的决策。我怀疑聚合会比继承贵得多。

Of all the decisions in the original Java class library, I consider this one of the more peculiar ones. I doubt that Aggregation would have been much more expensive than inheritance.

这篇关于从Vector继承的Java类Stack的负面影响是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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