Java advanced 07 nested class

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

So far, we have defined classes directly in java files. Such classes appear at the package level. Java allows nested definitions of classes.

Here we will explain how to nest and define another class in one class.

Nesting

Internal class

Java allows us to define a class within the class. If this class does not have a static modifier, such a nested class is calledInternal class(Inner class ).

An internal class is considered as an external object.Member. When defining internal classes, we also haveAccess permissionControl (public, private, protected ).

 

When using an internal class, we need to create an external object first. Since the internal class is a member of an external object, we can freely use the internal class within the object:

 Public   Class  Test {  Public   Static   Void  Main (string [] ARGs) {HUMAN me = New Human ("vamei"); Me. Drinkwater ( 0.3 );}}  Class  Human {  /**  * Inner class  */      Private   Class  Cup {  Public   Void Usecup ( Double  W ){  This . Water =This . Water- W ;}  Public   Double  Getwater (){  Return   This  . Water ;}  Private   Double "Water = 1.0" ;}  /**  * Constructor  */      Public Human (string N ){  This . Mycup = New  Cup ();  This . Name = N ;}  Public   Void Drinkwater ( Double  W) {mycup. usecup (w); system. Out. println (mycup. getwater ());}  Private  Cup mycup;  Private  String name ;} 

In the above example, the cup class is an internal class. This internal class has private access permission, so it can only be used within human. In this way, the cup class becomes a human class.Dedicated.

 

If we use other access permissions, internal classes can also be accessed from the outside, for example:

 Public   Class  Test {  Public   Static   Void  Main (string [] ARGs) {HUMAN me = New Human ("vamei" ); Me. Drinkwater ( 0.3 ); Human. Cup solocup = Me. New  Cup ();// Be careful here}}  Class  Human {  /**  * Inner class  */                                                                                                                                                                   Class  Cup {  Public   Void Usecup ( Double  W ){ This . Water = This . Water- W ;}  Public   Double  Getwater (){  Return   This  . Water ;}  Private   Double "Water = 1.0" ;}  /**  * Constructor  */     Public  Human (string N ){  This . Mycup = New  Cup ();  This . Name = N ;}  Public   Void Drinkwater ( Double  W) {mycup. usecup (w); system. Out. println (mycup. getwater ());}  Private  Cup mycup;  Private String name ;} 

Here, the internal class is the default access permission (package access permission ). We can access the human internal class cup in the test class and use this internal class to create objects. Note how to describe the type and use new during creation:

Human. Cup solocup = me. New cup ();

When creating an internal class object, we must create a cup object (Me. New) based on an external Class Object (me ). I will describe the meaning in the next section.

 

Closure

As you can see, when we directly create an internal class object, it must be based on an external class object. That is to say, internal class objects mustAttachmentIn an external class object.

 

Internal and external objects

At the same time,Internal Class ObjectAccessibleMember of the external class object to which it is attached(Even private members ). From another perspective, the internal class object comes with the environment information at the time of creation, that is,Closure (closure)Features. Refer to the python closure.

Let's look at the following example:

 Public   Class  Test {  Public   Static   Void  Main (string [] ARGs) {HUMAN me =New Human ("vamei" ); Human him = New Human ("Jerry" ); Human. Cup myfirstcup = Me. New  Cup (); Human. Cup mysecondcup = Me. New  Cup (); Human. Cup hiscup = Him. New  Cup (); system. out. println (myfirstcup. whoscup (); system. out. println (mysecondcup. whoscup (); system. out. println (hiscup. whoscup ());}} Class  Human {  /**  * Inner class  */      Class  Cup {  Public  String whoscup (){  Return Name; //  Access outer field  }}  /**  * Constructor */      Public  Human (string N ){  This . Name = N ;}  Public   Void  Changename (string N ){  This . Name = N ;}  Private  String name ;} 

Running result:

Vamei
Vamei
Jerry

 

In the preceding example, we use an internal class object to access the name Member of an external class object. When we create an internal class object based on different external objects, the obtained Environment Information also changes.

 

Nested static class

We can define it inside the class.Static class. Such a class is calledNested static class(Nested static class ).

We can directly create nested static class objects without attaching them to an object of an external class. Correspondingly, nested static classes cannot call External Object methods or read or modify external object data. The nested static class expands the namespace of the class, for exampleHuman. languliaN:

 Public   Class  Test {  Public   Static   Void  Main (string [] ARGs) {HUMAN. inclulian him = New  Human. Mongolian (); him. Shout ();}}  Class  Human { /**  * Nested class  */      Static   Class  Mongolian {  Public   Void  Shout () {system. Out. println ( "Oh... ho ..." );}}} 

When defining nested static classes, we can also have differentAccess permissionModifier.

 

Summary

Nested classes allow us to better organize classes

The internal class implements the closure.

 

Welcome to continue reading the "Java quick tutorial" SeriesArticle

 

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.