Java Fundamentals (15): Internal classes in Java

Source: Internet
Author: User

Q: What is an internal class?

A: The Inner class (Inner Class) is the class defined in another class. Corresponding to this, the class containing the inner class is called the outer class.

Q: Why do you define a class in another class? Clear refreshing cool independent of a kind of how good ah!!

A: The main functions of the inner class are as follows:

1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class

2. The method of the inner class can access all the data of the external class directly, including the private data

3. Functions implemented by internal classes can also be implemented using external classes, but sometimes it is easier to use internal classes

Q: How many internal classes are there?

A: The inner class can be divided into the following types:

    • member Inner class
    • Static Inner class
    • Method Inner Class
    • Anonymous inner class
//External class HelloWorld Public classHelloWorld {//Inner class inner, class inner inside class HelloWorld     Public classInner {//methods for inner classes         Public voidShow () {System. out. println ("Welcome to imooc!"); }    }     Public Static voidMain (string[] args) {//creating an external class objectHelloWorld Hello =NewHelloWorld (); //creating an inner class objectInner i = hello.NewInner (); //methods for invoking inner class objectsi.show (); }}

First, the member inner class in Java:

The most common inner class is the member inner class, also known as the ordinary inner class. Let's look at the following code:

The result of the operation is:

As we can see from the code above, the use of the members ' inner classes is as follows:

1. The Inner class is defined inside the Outer class and is equivalent to the position of a member variable of the Outer class, and the Inner class can use any access control, such as public, protected, private, etc.

2. The test () method defined in the Inner class can directly access the data in the Outer class without being affected by the access control, such as direct access to private property A in the Outer class

3, after defining the member inner class, you must use the Outer class object to create the inner class object, but not directly to the new inner class object, namely: The Inner class object name = Outer class object. New inner Class ();

4, after compiling the above program, you will find that two. class files have been generated

Where the second is the. class file for the outer class, the first is the. class file for the inner class, that is, the. class file for the member's inner class is always the case: the outer class name $ internal class name. class

In addition, friendly hints OH:

1. External classes cannot use the members and methods of the inner class directly.

You can first create an object of an inner class and then access its member variables and methods through the objects of the inner class.

2. If the outer class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variable of the external class, you can use the This keyword: the external class. This member property of the external class . Such as:

Operation Result:

Task

Little friends, let's do a practice to solidify the use of members ' internal classes!

The member inner class Inner is defined in the editor, and the property values of the outer and inner classes are output in the show method of the inner class.

Program Run Result:

//External class HelloWorld Public classhelloworld{//Private property of the external class name    PrivateString name ="IMOOC"; //member properties for external classes    intAge = -; //member Inner class inner     Public classInner {String name="Adoration Class"; //methods in the inner class         Public voidShow () {System. out. println ("name in the external class:"+Helloworld.this . Name); System. out. println ("name in the inner class:"+name); System. out. println ("Age in the outer class:"+Age ); }    }    //Test member Inner class     Public Static voidMain (string[] args) {//create an object of an external classHelloWorld o =NewHelloWorld (); //to create an object of an inner class       Inner inn = O.new Inner (); //call the Show method of an inner class objectinn.show (); }}

Second, the static inner class in Java:

Static inner classes are internal classes of static adornments, which are characterized by:

1. Static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external Class (). Members

2. If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name

3. When creating an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner class ();

Operation Result:

Task

Pro, let's do a practice test.

The static inner class sinner is defined in the editor, a variable score is defined in the inner class, and a static variable score with the same name is defined in the outer class, and the code is completed in lines 8th, 13, 21, complete the object creation of the static inner class, and call its method output value.

Operation Result:

//External Class Public classHelloWorld {//static variables in the outer class score    Private Static intScore = -; //Create a static inner class     Public Static classSinner {//variables in the inner class score        intScore = the;  Public voidShow () {System. out. println ("to access score in an external class:"+Helloworld.score); System. out. println ("to access the score in the inner class:"+score); }    }    //testing static Internal classes     Public Static voidMain (string[] args) {//directly create an object of the inner classSinner Si =NewSinner (); //call the Show methodsi.show (); }}

Third, the method inner class in Java:

The inner class of the method is the inner class defined in the method of the outer class, and the inner class of the method is visible only within the method, that is, it can be used only inside the method.

It is important to note that because the method inner class cannot be used outside of the methods of the outer class, the method inner class cannot use the access control and the static modifier .

Task

Pro, let's do a practice test.

The HelloWorld class is defined in the editor, there is a show method in the class, and a method inner class Minner is defined in the Show method, please complete the code in lines 17th, 20, 29.

Program Run Result:

//External Class Public classHelloWorld {PrivateString name ="Adoration Class"; //Show methods in external classes     Public voidShow () {//defining method Inner classes        classMinner {intScore = the;  Public intGetscore () {returnScore +Ten; }        }        //to create an object of a method inner classMinner mi =NewMinner (); //methods for calling inner classesMi.getscore (); System. out. println ("Name:"+ name +"\ n Bonus score:"+score); }    //test method Inner class     Public Static voidMain (string[] args) {//create an object of an external classHelloWorld mo =NewHelloWorld (); //methods for calling external classesmo.show (); }}

Java Fundamentals (15): Internal classes in Java

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.