在 Java 中,为什么 Stack 是一个具体类而 Queue 是一个接口? [英] In Java, why is it that a Stack is a concrete class whereas the Queue is an interface?

查看:46
本文介绍了在 Java 中,为什么 Stack 是一个具体类而 Queue 是一个接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Queue 的哪个子类是普通的"队列?

Which one of Queue's subclasses is a 'plain ordinary' queue?

推荐答案

(1) java.util.Stack 是 Java 1.0 的遗留类.它比 Collections 框架早很多年,坦率地说,它是许多方面可怕设计的一个例子.没有什么是事情应该是这样的.主要问题是Stack扩展了Vector,而Java中的所有继承都是公共继承,Vector的所有方法都可以在上使用>Stack 也是如此.因此,您可以检查 Stack 中的任何位置,从中间添加和删除元素,清除它,或者执行任何不应该属于堆栈抽象的一部分的其他任何事情,无需强制转换.与使用 QueueDeque 接口形成对比,通过它们只能使用适合堆栈的方法.

(1) java.util.Stack is a legacy class from Java 1.0. It predates the Collections framework by many years, and it's frankly an example of horrible design on many fronts. Nothing about it is the way things should be. The main problem is that Stack extends Vector, and as all inheritance in Java is public inheritance, all the methods of Vector are available on Stack as well. Therefore, you can inspect any position in a Stack, add and remove elements from the middle, clear it, or do any number of other things that should not be part of a stack abstraction, without a cast. Contrast that to using the Queue or Deque interfaces, through which only stack-appropriate methods are available.

(2) 真的没有普通队列这种东西,但是LinkedList 实现了Queue 没有任何特殊的语义,所以也许这就是你想要的.

(2) There really is no such thing as a plain ordinary queue, but LinkedList implements Queue without any special semantics, so maybe that's what you want.

这篇关于在 Java 中,为什么 Stack 是一个具体类而 Queue 是一个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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