简体   繁体   English

加载内部类Java

[英]Load Inner Class Java

I have this class in Java (it's from JaCoCo Project): 我在Java中有此类(来自JaCoCo Project):

public class MemoryMultiReportOutput implements IMultiReportOutput {

    private final Map<String, ByteArrayOutputStream> files = new HashMap<String, ByteArrayOutputStream>();

    private final Set<String> open = new HashSet<String>();

    private boolean closed = false;

    public OutputStream createFile(final String path) throws IOException {
        assertFalse("Duplicate output " + path, files.containsKey(path));
        open.add(path);
        final ByteArrayOutputStream out = new ByteArrayOutputStream() {
            @Override
            public void close() throws IOException {
                open.remove(path);
                super.close();
            }
        };
        files.put(path, out);
        return out;
    }

    public void close() throws IOException {
        closed = true;
    }

    public void assertEmpty() {
        assertEquals(Collections.emptySet(), files.keySet());
    }

    public void assertFile(String path) {
        assertNotNull(String.format("Missing file %s. Actual files are %s.",
                path, files.keySet()), files.get(path));
    }

    public void assertSingleFile(String path) {
        assertEquals(Collections.singleton(path), files.keySet());
    }

    public byte[] getFile(String path) {
        assertFile(path);
        return files.get(path).toByteArray();
    }

    public InputStream getFileAsStream(String path) {
        return new ByteArrayInputStream(getFile(path));
    }

    public void assertAllClosed() {
        assertEquals(Collections.emptySet(), open);
        assertTrue(closed);
    }
}

When I compile this class the Eclipse create MemoryMultiReportOutput.class and MemoryMultiReportOutput$1.class . 当我编译此类时,Eclipse将创建MemoryMultiReportOutput.classMemoryMultiReportOutput$1.class

First question: Why Eclipse create the MemoryMultiReportOutput$1.class ? 第一个问题:为什么Eclipse创建MemoryMultiReportOutput$1.class Eclipse considers the ByteArrayOutputStream out a InnerClass? Eclipse是否认为ByteArrayOutputStream out是InnerClass?

But my problem is, when I load the MemoryMultiReportOutput.class how can I load the all innerclasses present in parent class? 但是我的问题是,当我加载MemoryMultiReportOutput.class如何加载父类中存在的所有内部类?

To answer your first question: 要回答您的第一个问题:

final ByteArrayOutputStream out = new ByteArrayOutputStream() {
        @Override
        public void close() throws IOException {
            open.remove(path);
            super.close();
        }
    };

Here you are creating a subclass of the ByteArrayOutputStream on the fly, ie anonymous. 在这里,您正在动态创建ByteArrayOutputStream的子类,即匿名类。 This is why you have another .class file. 这就是为什么您还有另一个.class文件的原因。

To answer your second question: 要回答第二个问题:

You can only load parent inner classes, visible to the subclass, through the Superclass's instance object : 您只能通过超类的实例对象加载对子类可见的父内部类:

Superclass s = new Superclass();
Superclass.Subclass sub = s.new Subclass();

If the inner class is static ie a top-level nested class (since there is no such thing as inner static class) can be instantiated like this: 如果内部类是静态的,即顶级嵌套类 (因为没有内部静态类之类的东西)可以像这样实例化:

Superclass.Subclass s = new Superclass.Subclass();

and it does not require an object instance of the superclass. 并且它不需要超类的对象实例。

Hope this helps! 希望这可以帮助!

Your creating an anonymous inner class with the 您使用创建匿名内部类

new ByteArrayOutputStream()

That's why you see the MemoryMultiReportOutput$1.class file. 这就是为什么您看到MemoryMultiReportOutput$1.class文件的原因。

You don't need to do anything to load the inner classes. 您无需执行任何操作即可加载内部类。 That will happen automatically. 那将自动发生。

If your asking how to access the inner class from another class that's a bit different. 如果您询问如何从另一个类访问内部类,则有些不同。 You would need to mark it public or provide an accessor that would return an instance of the class. 您需要将其标记为public或提供一个访问器,该访问器将返回该类的实例。 Is that what you were asking? 那是你在问什么吗?

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

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