覆盖Java堆栈类的方法

发布于 2025-02-08 18:45:08 字数 813 浏览 4 评论 0原文

我创建了自己的类“神秘藏”,该类“神秘堆”从堆栈继承,并存储我在其他地方声明的“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命比纸薄 2025-02-15 18:45:08

我不确定您为什么要从堆栈继承而来,但是您对超级效力的问题非常简单。您应该使用 super.pop() [调用父级方法],而不是 this.pop() [调用此方法==递归调用]

    @Override
public MyItem pop() {
    if (this.isEmpty()) {
        return null;
    }
    pops++;
    return super.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]

    @Override
public MyItem pop() {
    if (this.isEmpty()) {
        return null;
    }
    pops++;
    return super.pop();
}
ぶ宁プ宁ぶ 2025-02-15 18:45:08

虽然您可以修复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 :使用Dequepushpoppeek成为 addfirst ,removefirstgetfirst < /code>根据文档,但我更喜欢addlastremovelastgetlast

stack.popdeque.removefirstdeque.removelast在空的情况下抛出异常。但是,Deque还带有pollfirstpolllast返回null而不是。这正是您要在这里实现的目标。

因此,摘要:使用Deque(以及Addflast, polllast and code> Arraydeque )实现) >。

While you can fix your pop method, I have an even better solution for you - stop using java.util.Stack completely. It's an ancient relic that has been poorly designed because it extends java.util.Vector instead of encapsulating it. That means I can create a Stack instance, fill it, and start removing items from the middle - because it inherits methods like removeElementAt from Vector.

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 and peek become addFirst, removeFirst and getFirst according to the documentation, but I prefer addLast, removeLast and getLast.

Like Stack.pop, Deque.removeFirst and Deque.removeLast throw an exception if empty. However, Deque also comes with pollFirst and pollLast which return null instead. That's exactly what you're trying to achieve here.

So summarizing: use Deque (and implementation ArrayDeque) with addLast, pollLast and getLast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文