“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
preface
Inner classes can be dispensed with everywhere they are used, but they can be used to make programs more concise, facilitating naming conventions and hierarchies.
An inner class is defined inside an outer class. Inner classes can be static and can be PROTECTED or PRIVATE. (External classes can’t: top-level classes can only use PUBLIC and DEFAULT.) No Publie class in a JAVA file can have a different class name than the file name.
I. Classification of inner classes
Member inner class,
Local inner class,
Static inner class,
Anonymous inner class (graph is to use, must master).
Member inner class
Exists as a member of an external class, along with properties and methods of the external class.
Instance variables of inner and outer classes can coexist.
Access the instance variable: this. Property in the inner class
The inner class accesses the instance variable of the outer class: the outer class name.this. property.
The outer of the outer class accesses the inner class, using out.inner.
Features of member inner classes:
1. As a member of an external class, an inner class can access the private members or attributes of the external class. (Even if the outer class is declared PRIVATE, the inner class inside it is visible.)
2. Use inner classes to define properties that are not accessible in outer classes. This gives the external class even less access than the external class private.
Note: An inner class is a compile-time concept and, once compiled, becomes a completely different class.
For an outer class named Outer and an inner class defined internally named inner. Outer. Class and outer$inner. Class.
3. Member inner classes cannot have static attributes
When creating inner class objects, note:
Inner s=new inner(); (Because the outer class knows which class inner is, it can generate objects.)
To generate (new) an inner class object outside the outer class, first create an outer class object (the outer class is available), and then generate an inner class object.
Outer o=new Outer();
The Outer, Inner = o.n in ew. Inner ().
Local inner class
Inner classes defined in methods are called local inner classes.
Like local variables, local inner classes are not preceded by the public and private modifiers, and are scoped to the code block that defines them.
Note:
Local inner classes can access not only external class instance variables, but also local constants of the external class
Local inner classes cannot be accessed directly from outside the class (making sure that local inner classes are invisible to the outside world).
Its local inner class can only be called in a method.
Static inner class
(Note: The first three inner classes are similar to variables, so reference variables can be compared.)
Static inner class definitions are defined static in a class, outside of any method.
A static inner class can access only static members of an external class.
Generating (new) a static inner class does not require an outer class member: this is the difference between a static inner class and a member inner class. Objects of static inner classes can be generated directly:
The Outer, Inner in = new Outer. Inner ();Copy the code
You don’t need to generate an external class object to do so. This effectively makes the static inner class a top-level class. Static inner classes cannot be defined using private.
Note: When a method naming conflict occurs between a class and an interface (or between interfaces), it must be implemented using an inner class.
Interfaces cannot fully implement multiple inheritance, but interfaces with inner classes can achieve true multiple inheritance.
Example:
For both classes, we have the same method:
class People
{
run();
}
interface Machine{
run();
}
Copy the code
There is a robot class:
class Robot extends People implement Machine.
Run () cannot be implemented directly.
interface Machine { void run(); } class Person { void run(){System.out.println("run"); } } class Robot extends Person { private class MachineHeart implements Machine { public void run(){System.out.println("heart run"); } } public void run(){System.out.println("Robot run"); } Machine getMachine(){return new MachineHeart(); } } class Test { public static void main(String[] args) { Robot robot=new Robot(); Machine m=robot.getMachine(); m.run(); robot.run(); }}Copy the code
Anonymous inner classes
An anonymous inner class is a special local inner class that implements an interface through an anonymous class.
IA is defined as an interface.
IA I=new IA(){};
Note: An anonymous inner class must be used after new to implicitly implement an interface or implement a class. There is no class name.
Because it is a local inner class, all the restrictions of a local inner class apply to it.
Anonymous inner classes are the only class without constructors.
Anonymous inner classes are automatically named Out$1.class by the system at compile time.
If an object is compiled as an interface, it runs as the class that implements that interface.
Because anonymous inner classes have no constructors, their use is very limited.