This article describes the creation and hook mechanism of Java internal class objects. Share it for your reference, as follows:
Although internal classes in Java are completely independent of their peripheral classes in state information (it can directly perform their functions through internal class instances), peripheral class objects are the basis for the existence of internal class objects.
When generating internal class objects, it is necessary to ensure that they can have peripheral class objects to hook, so that Java provides strict syntax for generating internal class objects.
There are two methods that are generally used to generate internal class objects.
Method1: Standard method using peripheral class instance.new internal class name().
Example 1:
public class Outer{ int no; class Inner implements InterfaceA{}}interface InterfaceA{}main(){Outer instanceA= new Outer();InterfaceA interfaceA=instanceA.new Inner() ;// Pay attention to the method of generating internal class instances, new peripheral class instance. Internal class name() . The purpose of this is to ensure that the internal class instance must have peripheral class instance hook.InterfaceA interfaceB=instanceA.new Outer();// Another instance of the internal class is generated, and it is also attached to the instance instanceA. }Method2 : Use the factory method to directly return an object that implements a certain interface (the object is generally an internal class).
Example 1:
public class Outer{ int no; class Inner implements InterfaceA{} public InterfaceA createInnerInstance{ return InterfaceA{ read(); } }}interface InterfaceA{public void read();}main(){Outer instanceA= new Outer();InterfaceA interfaceA=instanceA.createInnerInstance();//Note the method generated by the internal class instance, directly call a factory method and return an object that implements the InterfaceA interface. InterfaceA interfaceB=instanceA.createInnerInstance();//A new instance of the internal class is generated, and it is also attached to the instance instanceA. }Compared to the way of using the instance name .new class name(), the factory method appears more elegant and clear.
! Pay attention to the usage scenario of internal classes. Generally, it is to return an internal class object that implements a certain interface function to the outside, and then perform corresponding operations. (Since the members of the peripheral class are fully visible to the internal class, their convenience and advantages are very obvious)
Since the compiler automatically captures the peripheral class instance information when the inner class object is generated, Java also provides a .this method to call back peripheral class instances (this method is sometimes very important).
Example:
public class Outer{ class inner { public Outer callback(){return Outer.this}//callback() method returns a reference to the peripheral class object of the inner class object hook, using the syntax peripheral class name.this, pay attention to the return type of the function! }}For more Java-related content, readers who are interested in this site can view the topics: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.