关于局部变量的内联匿名类如何在后台工作?

问题描述

假设我们有以下类:

public class Foo {

    private final List<String> myFooList;

    public Foo(BarHelper barHelper) {
        myFooList = barHelper.getMyList();
    }

    public static void main(String[] args) {
        FooListRepository fooListRepository = new FooListRepository();
        Foo myFoo = new Foo(new BarHelper() {
            @Override
            public List<String> getMyList() {
                return fooListRepository.getList();
            }
        });

        System.out.println(myFoo.getMyFooList().toString());
    }

    public List<String> getMyFooList() {
        return myFooList;
    }

}

interface BarHelper {
    List<String> getMyList();
}

public class FooListRepository {

    private List<String> myBarList;

    public FooListRepository() {
        //initilize myBarList
    }

    public List<String> getList() {
        return myBarList;
    }
}

main 方法中的匿名类在内部究竟是如何工作的?

与 java 文档一样,将编译一个新类,但问题是如何处理 myTestList? java会以某种方式将它保存在匿名类中作为内部变量吗?

这行的背景到底发生了什么System.out.println(myFoo.getMyFooList().toString());

Java 是否以某种方式在后台调用 FooListRepository

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)