覆盖Java堆栈类的方法
我创建了自己的类“神秘藏”,该类“神秘堆”从堆栈继承,并存储我在其他地方声明的“ myitem”项目。
public class MyStack extends Stack<MyItem> {
private int pops = 0;
public MyStack() {
}
}
我想覆盖方法pop()
,然后这样做:
@Override
public MyItem pop() {
if (this.isEmpty()) {
return null;
}
pops++;
return this.super();
}
这是不起作用的,因为'java.util.stack'不是内部类
。
然后,我尝试使用pop()
而不是:
@Override
public MyItem pop() {
if (this.isEmpty()) {
return null;
}
pops++;
return this.pop();
}
但是AS pop()
是调用pop()
,我得到的是一个无限的递归调用和因此,stackoverflowerror
。
我不明白我应该如何覆盖它。
I have created my own class "MyStack" that inherits from Stack and stores items "MyItem" that I have declared elsewhere.
public class MyStack extends Stack<MyItem> {
private int pops = 0;
public MyStack() {
}
}
I want to override the method pop()
and I do it this way:
@Override
public MyItem pop() {
if (this.isEmpty()) {
return null;
}
pops++;
return this.super();
}
This doesn't work because 'java.util.Stack' is not an inner class
.
Then I try using pop()
instead:
@Override
public MyItem pop() {
if (this.isEmpty()) {
return null;
}
pops++;
return this.pop();
}
But as pop()
is calling pop()
, what I get is an infinite recursive call and, as a consequence, a StackoverflowError
.
I don't understand how I should override it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您为什么要从堆栈继承而来,但是您对超级效力的问题非常简单。您应该使用 super.pop() [调用父级方法],而不是 this.pop() [调用此方法==递归调用]
I'm not sure why you want inherit from Stack but your problem with overriding is quite simple. You should use super.pop() [calls parent method] instead of this.pop() [calls this method == recursive call]
虽然您可以修复
pop
方法,但我为您提供了更好的解决方案 - 完全停止使用java.util.stack
。这是一个古老的遗物,其设计不佳,因为它扩展了java.util.util.util.util.util.util
,而不是封装 it。这意味着我可以创建一个stack
实例,填充它,然后开始从中间删除项目 - 因为它从vector
中继承了remove> remove> remove> remove> remove> remove> emove>。
在 https://docs.oracle.com/en/java/java/javase/18/docs/api/java.base/java/java/util/stack.html :使用
Deque
。push
,pop
和peek
成为 addfirst ,removefirst
和getfirst < /code>根据文档,但我更喜欢
addlast
,removelast
和getlast
。像
stack.pop
,deque.removefirst
和deque.removelast
在空的情况下抛出异常。但是,Deque
还带有pollfirst
和polllast
返回null
而不是。这正是您要在这里实现的目标。因此,摘要:使用
Deque
(以及Addflast
, polllast and code> Arraydeque )实现) >。While you can fix your
pop
method, I have an even better solution for you - stop usingjava.util.Stack
completely. It's an ancient relic that has been poorly designed because it extendsjava.util.Vector
instead of encapsulating it. That means I can create aStack
instance, fill it, and start removing items from the middle - because it inherits methods likeremoveElementAt
fromVector
.The better solution is mentioned on https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Stack.html: use a
Deque
.push
,pop
andpeek
becomeaddFirst
,removeFirst
andgetFirst
according to the documentation, but I preferaddLast
,removeLast
andgetLast
.Like
Stack.pop
,Deque.removeFirst
andDeque.removeLast
throw an exception if empty. However,Deque
also comes withpollFirst
andpollLast
which returnnull
instead. That's exactly what you're trying to achieve here.So summarizing: use
Deque
(and implementationArrayDeque
) withaddLast
,pollLast
andgetLast
.