What is this type of initialization called and why is it used? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

This question already has an answer here:

Animal is a user defined class here.

Animal D = new Animal("Leo") {          @Override public void makeNoise() {                System.out.println("Roar!");         }  };   D.makeNoise(); 

回答1:

Its called an anonymous class and used to define the class and any overridden methods at the same time.



回答2:

That is an anonymous class. For details about anonymous classes and why they are useful, see this tutorial on anonymous classes.



回答3:

This is used to override the initial

Animal.makeNoise() 

method with a custom one for just this instance.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!