简体   繁体   English

实现了属于枚举接口的hasMoreElements()的具体类是什么?

[英]What is the concrete class that has implemented the hasMoreElements() that belongs to the Enumeration Interface?

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

class FileEnumerator implements Enumeration<InputStream> {

    private Enumeration<String> files;

    public FileEnumerator(Vector<String> files) {
        this.files = files.elements();
    }

    @Override
    public boolean hasMoreElements() {
        return this.files.hasMoreElements(); //  ------- >>>> This Method ??
    }

    @Override
    public InputStream nextElement() {
        try {
            return new FileInputStream(this.files.nextElement().toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

public class App {
    public static void main(String args[]) throws IOException {
        Vector<String> files = new Vector<>();
        files.add("file1.txt");
        files.add("file2.txt");
        files.add("file3.txt");
        SequenceInputStream sequenceInputStream = new SequenceInputStream(
                new FileEnumerator(files));
        int c;
        while ((c = sequenceInputStream.read()) != -1) {
            System.out.print((char)c);
        }
        sequenceInputStream.close();
    }
}

I am totally confused .. I don't know where the hasMoreElement() method in Enumeration interface has been implemented to return true or false .. since in this program didn't include it's only concrete class which is StringTTokenizer http://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html 我完全糊涂了..我不知道Enumeration接口中的hasMoreElement()方法在哪里实现返回true或false ..因为在这个程序中没有包含它的唯一具体类是StringTTokenizer http:// docs .oracle.com / JavaSE的/ 8 /文档/ API / JAVA / UTIL / Enumeration.html

Also there is not such a method stand alone in Vector class .. 另外在Vector类中没有这样的方法。

I just don't get it ... 我只是不明白......

Here is the overridden method in Vector class. 这是Vector类中重写的方法。

public boolean hasMoreElements() {
            return count < elementCount;
}

You called files.elements(); 你调用files.elements(); The method that I mentioned above, is in this file.elements() method. 我在上面提到的方法是在这个file.elements()方法中。

Hope this helps 希望这可以帮助

I believe you're questioning this, 我相信你在质疑这个,

@Override
public boolean hasMoreElements() {
    return this.files.hasMoreElements(); //  ------- >>>> This Method ??
}

Seems like an example of the Decorator pattern on the Vector#elements() Enumeration (and that is the concrete implementation you are looking for). 看起来像Vector#elements() Enumeration上的Decorator模式的一个例子(这是你正在寻找的具体实现)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM