简体   繁体   English

如何使用迭代器为两个类实现接口

[英]How to implement an interface for two classes with an iterator

I'm trying out Interfaces in java and I want to implement a common interface for a really simple stack, with pop() and push() methods and an iterator.我正在尝试 java 中的接口,我想为一个非常简单的堆栈实现一个通用接口,使用pop()push()方法和一个迭代器。

The problem is that I don't know how to specify the iterator in the interface.问题是我不知道如何在接口中指定迭代器。 No matter which way I try, I get无论我尝试哪种方式,我都会得到

Main.java:32: error: for-each not applicable to expression type
                for (Integer i : ss)
                                 ^
required: array or java.lang.Iterable
found:    Stack<Integer>

The code is as follows:代码如下:

interface Stack<T> {

    boolean push(T t);

    boolean pop();
    
    //Iterator<T> iterator(); // How to indicate it needs, and will have, an iterator?
}
public class DynamicStack<T> implements Iterable<T>, Stack<T>
{
    // implementation-specific variables go here

    public DynamicStack() {
        //...
    }

    public boolean push(T t) {
        //...
    }

    public boolean pop() {
        //...
    }
    
    private class StackIterator implements Iterator<T> {
        DynamicStack<T> stk;
        //...
        
        // Iterator constructor
        private StackIterator(DynamicStack<T> stk)
        {
            //...
        }

        public boolean hasNext()
        {
            //...
        }

        public T next() throws NoSuchElementException
        {
            //...
        }

        public void remove() throws UnsupportedOperationException
        {
            throw new UnsupportedOperationException(); // I chose not to implement this one
        }
    }

    // Iterator method
    public Iterator<T> iterator()
    {
        return new StackIterator(this);
    }
}
public class StaticStack<T> implements Iterable<T>, Stack<T>
{
    // implementation-specific variables go here

    public StaticStack()
    {
        //...
    }

    public boolean push(T t)
    {
        //...
    }

    public boolean pop()
    {
        //...
    }

    private class StackIterator implements Iterator<T>
    {
        StaticStack<T> stk;
        //...

        private StackIterator(StaticStack<T> stk)
        {
            //...
        }

        public boolean hasNext()
        {
            //...
        }

        public T next() throws NoSuchElementException
        {
            //...
        }

        public void remove() throws UnsupportedOperationException
        {
            //...
        }
    }

    // Iterator method
    public Iterator<T> iterator()
    {
        return new StackIterator(this);
    }
}

Main simply does this, after creating a few stacks of each type and adding a few elements:在为每种类型创建几个堆栈并添加一些元素之后,Main 只是简单地执行此操作:

public static void showStuff(Stack<Integer> ss)
{
    for (Integer i : ss)
        System.out.print(i+" ");
    System.out.println();
}

In your test class, you are operating against Stack interface, so that is the one that needs to conform to Iterable .在您的测试 class 中,您正在针对Stack接口进行操作,因此这是需要符合Iterable的接口。 In this case it doesn't help if StaticStack or DynamicStack implement it if Stack does not.在这种情况下,如果Stack没有, StaticStackDynamicStack实现它也无济于事。

To get Stack to be able to be used as Iterable just change your Stack to extend Iterable:要使 Stack 能够用作 Iterable,只需更改您的 Stack 以扩展 Iterable:

public interface Stack<T> extends Iterable<T> {    

    boolean push(T t);

    boolean pop();        
}

and

public class StaticStack<T> implements Stack<T>

and the code runs just fine:并且代码运行得很好:

public class Tester {
  public static void main(String args[]) {
    Stack<Integer> ss = new StaticStack<>();
    for (Integer i : ss)
        System.out.print(i+" ");
    System.out.println();
  }
}

You need you class to implement Iterable<T> , which has the iterator() method, which returns Iterator<T> .您需要 class 来实现Iterable<T> ,它具有iterator()方法,该方法返回Iterator<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道两个类是否实现一个公共接口? - How to know if two classes implement a common interface? 如何分离实现相同接口的两个类的函数模拟? - How to separate function mocks of two classes that implement the same interface? 如何为实现相同接口的两个类制作双向适配器? - How to make bidirectional adapter for two classes that implement the same interface? 如何注入实现同一接口的两个不同类的两个实例? - How to inject two instances of two different classes which implement the same interface? 在Java中,如何区分实现相同接口的类? - In Java, how to distinguish classes that implement the same interface? 如何在类列表中实现不常用的方法/接口? - How to implement infrequent method/interface in list of classes? 如何给两个不同的类相同的接口? - How to give two different classes the same interface? 如何在两个类之间拆分接口的实现 - How to split the implementation of an interface between two classes 如何创建两个具有相同名称,签名和返回类型的方法的类,就像它们实现相同的接口一样 - How to make two classes which both have method with the same name, signature and return type behave like they would implement the same interface 如何使用迭代器从Java中的两个类检索对象 - How to use iterator to retrieve objects from two classes in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM