# # #面向对象 (Package 2) # # #
Inner classes in 1.Java
1) What is an inner class: The Inner Class (Inner Class) is the class that is defined in another class. Corresponding to this, the class containing the inner class is called the outer class.
2) Why should one class be defined in another class?
The main functions of the inner class are as follows:
The 1> inner class provides a better encapsulation that hides inner classes within external classes and does not allow other classes in the same package to access the class
The method of the 2> inner class can access all the data of the external class directly, including the private data
The functionality implemented by the 3> inner class can be implemented using an external class, but it is sometimes easier to use internal classes
3) There are several internal classes: member inner class, Static inner class, method inner class, Anonymous inner class
member Inner class in 2.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:
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 external 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. Such as:
Example: Defines the member inner class Inner in the editor and outputs the property values of the outer and inner classes in the show method of the inner class.
3. Static inner classes 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 (). Member
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 you create an object of a static inner class, you do not need an object of the outer class, you can directly create
Inner class object name = New inner class ();
Cases:
4. Methods inner Classes 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.
PS: Because the method inner class cannot be used outside the method of the outer class, the method inner class cannot use the access control and the static modifier.
5. The random method of the math class
Example: guessing a number game
6. Making Documentation Notes
Package example.make_api;/** * Current Arraytool This class is a tool class for array manipulation, which provides the ability to iterate through an array and get the maximum value of an array and the first occurrence of an index in an array. * @author Dream Bamboo * @ Version V1.0 */public class Arraytool {/** * The purpose of privatization is to not allow the outside world to create Objects */private Arraytool () {}/** * Currently this method is for the function of array traversal * @para M arr is an array that needs to be traversed */public static void PrintArray (int[] arr) {System.out.print ("["); for (int x = 0; x < arr.length; x + +) {//To determine if (x==arr.length-1) {System.out.println (arr[x]+ "]");} else {System.out.print (arr[x]+ ",");}}} /** * The current Getmax method is the method that gets the maximum value in the array * @param arr needs to traverse the array * @return return to the maximum value in the array */public static int Getmax (int[] arr) {//define One Reference, assuming the maximum value int max = arr[0];//Traverse other elements for (int x = 1; x < arr.length; x + +) {//Determine if (Arr[x] > max) {max = arr[x];}} return Max;} /** * The current method is to find the index of an element of an array that first appears in the array * @param arr * @param value * @return */public static int getindex (int[] Arr,int Valu e) {//assume that int index =-1 is not found;//traverse array for (int x = 0; x < arr.length; x + +) {//Determine if (arr[x] = = value) {//assigns the current x index to Indexindex = x; break;}} return index;}}
Package example.make_api;/** * Requirements: Customizing an array, traversing an array * Creating a document Description: * You can define a Arraytool, array manipulation tool class * in the tool class, you must document the comments to allow parsing into documents * In the Array tool class Arrayt OOL, which provides traversal capability, gets the maximum value of the index in the array to find the first occurrence of an element * * * @author Administrator * */public class Arraydemo {public static void Ma In (string[] args) {//define array, statically initialize int[] arr = {57,69,13,24,80};//test/Traverse function Arraytool.printarray (arr);//MAX int max = Arraytool.getmax (arr); SYSTEM.OUT.PRINTLN ("Maximum value is:" +max);//The index of the first parameter in the array that the element appears in int index = Arraytool.getindex (arr, 13); System.out.println ("Index is:" +index);}}
Two class files found at this time
Remove Package Example.make_api from tool
Win+r Call out DOS
# # # #END # #
Related articles:
Introduction to Java Tutorial (v) Basic Syntax (3)
Introduction to Java Tutorial (vi) Object-oriented (encapsulation)