Creation and hook Mechanism Analysis of class objects within Java Development, and department hook

Source: Internet
Author: User

Creation and hook Mechanism Analysis of class objects within Java Development, and department hook

This article describes how to create and hook Java internal class objects. We will share this with you for your reference. The details are as follows:

Although the internal class in Java is completely independent of its peripheral class in the state information (its function can be directly executed through the internal class instance ), however, peripheral class objects are the basis for the existence of internal class objects.

When generating internal class objects, you must ensure that they have peripheral class objects for hook, so that java provides strict syntax for generating internal class objects.

Generally, two methods are used to generate internal class objects.

Method1:Use the standard method of the peripheral class instance. new internal 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 (); // pay attention to the method generated by the internal class instance, new peripheral class instance. the internal class name () is used to ensure that the internal class instance has a peripheral class instance hook. interfaceA interfaceB = instanceA. new Outer (); // an internal class instance is generated and attached to instance instanceA .}

Method2: Use the factory method to directly return the objects that implement an interface (this object is generally an internal 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 (); // pay attention to the method generated by the internal class instance. A factory method is called directly and an object implementing the InterfaceA interface is returned. InterfaceA interfaceB = instanceA. createInnerInstance (); // an internal class instance is generated and attached to the instance instanceA .}

The factory method is more elegant and clear than the Instance name. new Class Name () method.

! Pay attention to the use cases of internal classes. Generally, an internal class object that implements the functions of an interface is returned to the outside and then the corresponding operations are performed. (Because the members of the peripheral class are completely visible to the internal class, their convenience and advantages are obvious)

When an internal class object is generated, the compiler automatically captures the information of its peripheral class instance, so java also provides. this method calls back peripheral class instances (this method is sometimes very important ).

Example:

Public class Outer {class inner {public Outer callback () {return Outer. this} // The callback () method returns a reference to the peripheral Class Object of the internal Class Object hook, using the syntax peripheral class name. this, pay attention to the function return type! }}

For more java-related content, refer to this topic: introduction to Java Object-Oriented Programming and advanced tutorials, Java data structure and algorithm tutorials, Java DOM node skills summaries, Java files and directories, and java cache operation tips

I hope this article will help you with java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.