Java Learning Series (23) Java Object-oriented partial class

Source: Internet
Author: User



First, preface

The inner class is also called a parasitic class, which is to put a class inside a class (that is, the upper-level program unit of the inner class is the class) definition, as a member of the outer class. Inner classes are mainly defined in several forms: static internal classes, non-static inner classes, anonymous inner classes (i.e., parasitic classes without names). The benefit of the inner class is that the inner class can be directly outside the class (including private) members, and vice versa. Below we have some examples to explain in detail the use of internal classes in Java and several definitions of the form of mutual invocation.


Ii. Description of the case

(1) Anonymous inner class: an instance of an anonymous inner class (Implementation class) is created immediately when the program creates an anonymous inner class.

Interface Ibreathe {void breathe ();} /** * Anonymous internal class usage, defined as follows: *  * New Interface () | Parent class constructor < parameters >) {  *//Class body part  ... * };  *  * @author [* Reproduced yesterday *] [email protected] * @since version 1.0 * @datetime April 29, 2015 PM 10:17:40 */public Class Anonymous {//Here is equivalent to creating an anonymous implementation class for interface ibreathe, and creating an instance of an anonymous inner class//Assigning an instance of an implementation class to an interface variable, which belongs to an upward transformation ibreathe breathe = new Ibreathe () {// All abstract methods in the interface must be implemented @overridepublic void Breathe () {System.out.println ("Breathe fresh air ...");}; public static void Main (string[] args) {Anonymous Anonymous = new Anonymous (); Anonymous.breathe.breathe ();}}

Note the following points:
1) As long as the parent class is an abstract class or an interface, the methods in its subclasses can be implemented using anonymous inner classes, which in turn means that the use of an anonymous inner class has a precondition: it must be shown to inherit a parent class or implement an interface.
2) An anonymous inner class must implement all the abstract methods in an interface or abstract class.
3) Anonymous inner classes are suitable for creating classes that need to be used only once.
4) An anonymous inner class cannot have a constructor and the program cannot access it later because it does not have a class name.


(2) Static inner class: It belongs to the static member of the outer class (the host Class), so it cannot access the non-static members (properties, methods) of the outer class.

/** * External Class (host Class) contains static inner class as a static member of Outer class *  * @author [* Reproduced yesterday *] [email protected] * @since version 1.0 * @datetime April 29, 2015 PM 11:00:51 */public class Out {private static String name = "Zhang San";p rivate String sex = "Male"; static class in {private static String name = "John Doe";p ublic Void info () {//static inner class cannot access non-static members of external class, so the following sentence will compile not through//System.out.println ("sex =" of the outer class + sex); System.out.println ("in info method called, Name:" + name);}} public static void Main (string[] args) {//Print results (compiler follows the nearest principle): In the Info method is called, Name: John Doe//This is just a package that takes an external class as a static inner class. So in front add not to add out. Are the same as new in (). info (); new out.in (). info ();}}

If you want to access the members of the static inner class outside the outer class: out.in in = new out.in (); In.info (); Of course, static inner classes can also derive subclasses: Class Insub extends Out.in {}, written and invoked in the same way as normal classes, no longer mentioned here. Here's a look at the key to the non-static inner class definition and use.


(3) Non-static inner class: Before creating an instance of a non-static inner class, you must first create an external class instance, which means that the non-static inner class must be parasitic in an instance of the outer class (Outer.this). Therefore, before you can create an instance of a non-static inner class, you must first create an external class instance.

/** * Use of non-static inner classes *  * @author [* Reproduced yesterday *] [email protected] * @since version 1.0 * @datetime April 29, 2015 afternoon 11:26:06 */public Class Outer {int number = 10;//defines a non-static inner class Inner {int number = 100;public void info () {int number = 1000; SYSTEM.OUT.PRINTLN ("the info () method of the inner inner class is called ~"); System.out.println (the value of the number variable is: "+"),//The local variable of the method, so it is 1000system.out.println ("The value of the number variable is:" + this.number);// Points to a parasitic class member, so 100system.out.println (the value of the number variable is: "+ Outer.this.number);//points to the host class member, so 10}}/** * accesses the members of the parasitic class by means of an external class (properties, method) */public void info () {System.out.println ("the info () method of the outer external class is called ~"); Inner Inner = new Inner (); Inner.info (); System.out.println (Inner.number);} public static void Main (string[] args) {new Outer (). info ();}}
If you use an inner class outside the outer class, you must first create an instance of the outer class before you can create an instance of the non-static inner class.
Outer Outer = new Outer ();
Outer.Inner in = Outer.new Inner ();
equivalent to = = "Outer.Inner in2 = new Outer (). New Inner ();

Of course, non-static inner classes can also derive subclasses, as follows:

Non-static inner class derived subclass class Innersub extends Outer.Inner {public innersub () {//Because Outer.Inner is a non-static inner class, you must use "host Object" to invoke its constructor new Outer (). super ();}}


Iii. Summary

1) The inner class with the static modifier belongs to the outer class itself, and the inner class without the static adornment belongs to the class instance.

2) Remember who the method is and call it with whom.

3) There is no static modification of the inner class, "must be parasitic in an instance of the outer class", whereas the other is parasitic in the outer class itself.

4) Static internal classes are parasitic within the class itself, so there is no need for programmers to ignore the host. -The outer class is treated as a package of static inner classes.

5) Non-static inner class derived subclasses: Because the constructor of the subclass must call the parent class constructor one time, the host object must be used in the subclass constructor to invoke its constructor.


Iv. concluding remarks

About the definition and usage of the inner classes in Java. Here are some of the things that follow: regular expressions, Java performance Optimizations, learning JVMs, and more.


Java Learning Series (23) Java Object-oriented partial class

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.