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

查看:23
本文介绍了从 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现在绑定到Vector,通常会避免使用ArrayLists等.

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

第三个问题是您无法轻松提供自己的堆栈实现,并且堆栈支持非常非堆栈的操作,例如从特定索引中获取元素,包括潜在的索引异常.作为用户,您可能还必须知道堆栈的顶部是在索引 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天全站免登陆