Measure the test taker's knowledge about the functions of anonymous internal classes.
Measure the test taker's knowledge about the definition formats of anonymous internal classes.
3. Details:
Internal class: there is another class inside a class called internal class, so the anonymous internal class, if a class is used only once in the entire operation, it can be defined as an anonymous internal class. The anonymous internal class is developed based on the abstract class and interface.
Interface A {public void printinfo ();} Class B implements a {// implementation interface public void printinfo () {system. Out. println ("Hello world !!! ") ;}} Class X {public void fun1 () {This. fun2 (New B ();} public void fun2 (A) {. printinfo () ;}} public class noinnerclassdemo01 {public static void main (string ARGs []) {new x (). fun1 (); // instantiate the X object and call the fun1 () method }}
If Class B is used only once at this time, is it necessary to define a separate class for it? In this case, you can use an anonymous internal class to solve the problem.
Interface A {public void printinfo ();} Class X {public void fun1 () {This. fun2 (new A () {public void printinfo () {system. out. println ("Hello world !!! ") ;}}) ;}Public void fun2 (A) {. printinfo () ;}} public class noinnerclassdemo02 {public static void main (string ARGs []) {new x (). fun1 (); // instantiate the X object and call the fun1 () method }}
Summary:
1. In actual Java Development, various development frameworks are often used, and a large number of anonymous internal classes are referenced in the frameworks.
2. Anonymous internal classes are developed based on abstract classes and interfaces.