Java Vamei Quick Tutorial 19 nested classes

Source: Internet
Author: User
Tags closure

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

So far, we have defined the class directly in the Java file. Such a class appears at the package level. Java allows nested definitions of classes.

Here's how to nest a class to define another class.

Nesting

Inner class

Java allows us to define a class within a class. If this class does not have a static modifier, then such a nested inner class is called an inner class (inner class).

An inner class is considered to be a member of an external object. When defining internal classes, we also have access control (public, private, protected).

When using inner classes, we create external objects first. Since the inner class is a member of an external object, we can freely use the inner 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 example above, the Cup class is an inner class. The inner class has private access, so it can only be used inside human. In this way, the Cup class becomes a class dedicated to the human class.

If we use other access rights, internal classes can also be accessed externally, such as:

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 inner class is the default access permission (Package access). We can access the internal class cup of human in the test class and use that inner class to create the object. Note how the types are described and the use of new when we create them:

Human.cup Solocup = Me.New Cup ();

when we create an inner class object, we must base it on an external class object (me) and create a cup object (me.new) from the outer classobject. I'll tell you what it means in the next section.

Closed Package

As you can see, when we create an inner class object directly, it must be based on an outer class object. In other words , an inner class object must be attached to an outer class object .

Internal objects and external objects

At the same time, an inner class object can access the members of the outer class object to which it is attached, even if it is a private member. From another perspective, the inner class object comes with the environment information created, that is, the closure (closure) attribute in other languages. Refer to Python closures

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;}

Operation Result:

Vamei
Vamei
Jerry

In the example above, we access the name member of the Outer class object through the inner class object. When we create inner class objects based on different external objects, the environment information we get will change as well.

Nested static Classes

We can define the static class inside the class. Such classes are called nested static classes (nested static Class).

Instead of attaching to an object of an external class, we can directly create an object of the nested static class. Accordingly, nested static classes cannot invoke methods of external objects, nor can they read or modify the data of external objects. In effect, nested static classes extend the namespace of the class (name space), such as the following human.mongolian:

public class test{public    static void Main (string[] args)    {        Human.mongolian 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 different access modifiers.

Summarize

Nested classes allow us to better organize classes

The inner class implements the closure

Java Vamei Quick Tutorial 19 nested classes

Related Article

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.