Java development of internal class object creation and hook mechanism analysis

  • 2020-12-26 05:43:49
  • OfStack

This article describes the Java inner class object creation and hook mechanism. To share for your reference, the details are as follows:

Although the inner class in Java is completely independent of its outer class in state information (its functions can be performed directly through the inner class instance), the outer class object is the basis for the existence of the inner class object.

When the inner class object is generated, it must be ensured that it can be attached by the outer class object (hook), thus java provides the strict syntax of inner class object generation.

There are two common ways to generate inner class objects.

Method1: Standard method for using peripheral class instances.new inner class name ().

Example 1:


public class Outer{
   int no;
  class Inner implents InterfaceA{}
}
interface InterfaceA{}
main()
{
Outer instanceA= new Outer();
InterfaceA interfaceA=instanceA.new Inner() ;// Notice the methods generated by the inner class instance, new  Peripheral class instance . The internal name of the class ()  , the purpose of this is to guarantee the inner class instance 1 There must be a peripheral class instance hook.
InterfaceA interfaceB=instanceA.new Outer();// Generate again 1 An instance of an inner class, and is also attached to the instance instanceA On. 
}

Method2: Uses the factory method to directly return an object that implements an interface 1 (typically an inner class).

Example 1:


public class Outer{
   int no;
  class Inner implents InterfaceA{}
  public InterfaceA createInnerInstance{
    return InterfaceA{
        read();
    }
   }
}
interface InterfaceA{public void read();}
main()
{
Outer instanceA= new Outer();
InterfaceA interfaceA=instanceA.createInnerInstance() ;// Notice the methods generated by the inner class instance , Directly called 1 Three factory methods, return 1 An implementation InterfaceA The object of the interface. 
InterfaceA interfaceB=instanceA.createInnerInstance();// Generate again 1 An instance of an inner class, and is also attached to the instance instanceA On. 
}

The factory method is more elegant and clear than using the instance name.new class name ().

! Note the usage scenario of inner classes. 1 usually returns an inner class object that implements some interface function to the outside, and then performs the corresponding operation. (Since the members of the peripheral class are all visible to the inner class, its convenience and advantage are obvious by 10 points)

java also provides.this style callbacks to peripheral class instances (this method is sometimes 10 points important), since the compiler automatically captures the peripheral class instance information when the inner class object is generated.

Example:


public class Outer
{
  class inner
  {
    public Outer callback(){return Outer.this}//callback() Method returns an inner class object hook A reference to a peripheral class object, using syntax   Outside the class name .this .   Notice the return type of the function! 
  }
}

For more java interested readers, please refer to: Java Object-oriented Programming Introduction and Advanced Tutorial, Java Data Structure and Algorithm Tutorial, Java Operation DOM Node Skills Summary, Java File and Directory Operation Skills Summary and Java Cache Operation Skills Summary.

I hope this article has helped you with the java programming.


Related articles: