有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么返回值对象在URLClassPath的getResources函数中有两个额外的字段?

我正在读spring boot的源代码。我在阅读SpringFactoriesLoader.loadFactoryNames时发现了一个问题。URLClassPath.getResources返回Enumeration<Resource>的实现,但该值有额外的两个字段namecheck。那么,何时将这两个字段添加到返回值中

public Enumeration<Resource> getResources(final String var1, final boolean var2) {
        return new Enumeration<Resource>() {
            private int index = 0;
            private int[] cache = URLClassPath.this.getLookupCache(var1);
            private Resource res = null;

            private boolean next() {
                if (this.res != null) {
                    return true;
                } else {
                    do {
                        URLClassPath.Loader var1x;
                        if ((var1x = URLClassPath.this.getNextLoader(this.cache, this.index++)) == null) {
                            return false;
                        }

                        this.res = var1x.getResource(var1, var2);
                    } while(this.res == null);

                    return true;
                }
            }

            public boolean hasMoreElements() {
                return this.next();
            }

            public Resource nextElement() {
                if (!this.next()) {
                    throw new NoSuchElementException();
                } else {
                    Resource var1x = this.res;
                    this.res = null;
                    return var1x;
                }
            }
        };
    }

我正在用Intellij调试程序,结果是

Debug Result


共 (1) 个答案

  1. # 1 楼答案

    在本例中,Result实例是一个匿名类,它捕获getResources参数(final String var1, final boolean var2)的值