Inner class
You can define another class within one class to be an inner class or nested class, such as:
1 classouter{2 ... ..3 classinner{4 ....5 }6 }7 classouter1{}//This Inner1 is not an inner class of Outer1, and is not in the Outer1 's class body brackets8 classinnter1{9 ...Ten } OneClass outer2{//Inner2 Although in the Outer2 method, but still in the Outer2 of the class body in parentheses, is the inner class A ... - Public voidTest () { - classinner2{ the .... - } - } -}
What are the problems and benefits of the inner class:
- Issue: External classes do not use or create external class objects, they cannot create intrinsic class objects directly
- Benefits: Private properties of external classes, internal classes can be used, and do not destroy encapsulation, which is why internal classes exist in the important reason
Attributes of inner classes
- The characteristics of inner classes are similar to those of variable concepts.
- The position of the inner class is similar to the variable position
- Variables can be divided into instance variables (member variables), local variables, static variables
- Internal classes are similar to variables and can be divided into four types
Classification of inner classes
Static inner class, member inner class, local inner class, Anonymous inner class
member Inner class
1. Using a member's inner class is like using a member variable, and in the main method of this class, the member inner class is used directly, without the name of the outer class to access. However, in the main method of other classes to use the member inner class, you need to use the external class. Internal classes to access.
2. Using an external class name to access the inner class also because an inner class may have different external classes
3. Creating a member inner class object must first create an object of the outer class, because the member inner class accesses the members of the outer class
4. The member inner class can access all properties of the external class
5. Static properties cannot be defined in a member's inner class
6. When a member of an external class has the same name as a local variable in an inner class member, it is accessed in three different ways
- Accessing a local variable directly using the variable name
- Use the this. Variable name to access members of the inner class
- Use the external class name. This variable name accesses members of the external class
Static Inner class
- An inner class defined as static on a member's location, called a statically inner class
- Static inner classes are similar to static variables, and modifiers that modify static variables can also decorate static inner classes
- Static internal classes are similar to static methods, static properties, access to static methods, static properties are accessed using the class name. Property name/method name, but in this class you can access it without the class name. The same is true for static inner classes, where access is not added to an external class name in this class of methods, and external class names are added to the methods of other classes. The internal class name to access.
- Static internal classes can only access static members of external classes and members of this inner class
- Non-static members of external classes cannot be accessed in static inner classes
Anonymous inner class
1. The anonymous inner class is a special local inner class that can be converted to an anonymous inner class
2. The local inner class must have 3 conditions to be converted to an anonymous inner class
- The first is a local inner class, which is also a private inner class
- The local inner class implements an interface or inherits a parent class
- Local inner classes create only one object at a time
3. An anonymous inner class is simply a class that implements an interface without explicitly declaring a class name to implement an interface.
4. Anonymous inner classes cannot define a constructor method because the anonymous inner class is a special local inner class that does not have a class name.
5. Anonymous inner classes can access local variables of external classes, but also require that local variables of the outer class be final for access
6. Anonymous inner classes can access properties of external classes
7. Anonymous inner classes can be created in a method of a class, and can also be implemented in the main method of a class.
8. When the inherited parent class has the same name as the method in the implemented interface, the same argument list, and the same return type, the subclass writes only one method implementation.
9. When an abstract method in an inherited parent class has the same name as a method in the implemented interface, but the return type is different, the subclass must write out each method, but such a set of methods has the same name, the return type is not the same method, and does not constitute an overload, then a compilation error occurs
Java Face Object (vi)--inner class, Anonymous inner class