Anonymous class with interface

后端 未结 4 1892
挽巷
挽巷 2021-01-16 14:24

I am confused about the concept of interface when dealing with anonymous inner class. As far as I know that you can\'t instantiate an interface in Java, so the following sta

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 15:21

    You're defining an inner class with a sequentially assigned name like 1, 2, 3 etc. At the same time you're instantiating the inner class with the new keyword. You don't care about the name of the inner class because you're using it anonymously. If you look in your bin directory you'll see a class file for each of the anonymous definitions. For example if you used an anonymous class in a class, Foo, you would have Foo.class and Foo$1.class created for you. I believe this means that you could instantiate more of the anonymous classes at a later date using reflection.

提交回复
热议问题