Internal Class Access Characteristics
* a: Internal classes have direct access to members of external classes, including private ones.
* b: To access members of internal classes, external classes must create objects.
* External class name. Internal class name. Object name = External class object. Internal class object;
The summary is just to treat the internal class as a variable corresponding to the location and access permission.
demo1
demo2: Private internal classesclass Demo1_InnerClass { public static void main(String[] args) { //Inner i = new Inner(); //i.method(); //External class names. Internal class names. Object names = external class objects. Internal class objects Outer.Inner oi = new Outer().new Inner(); oi.method(); } } class Outer { private int num = 10; class Inner { public void method() { System.out.println(num); } } }
Static inner classclass Demo2_InnerClass { public static void main(String[] args) { //Outer.Inner oi = new Outer().new Inner(); //oi.method(); Outer o = new Outer(); o.print(); } } class Outer { private int num = 10; private class Inner { public void method() { System.out.println(num); } } public void print() {//Using a method area call Inner i = new Inner(); i.method(); } }
Format: External class name. Internal class name object name = external class name. Internal class object is used to write new in front.
The Problem of Accessing Local Variables by Local Internal Classesclass Demo1_InnerClass { public static void main(String[] args) { Outer.Inner oi = new Outer.Inner(); oi.method(); Outer.Inner2.print(); } } class Outer { static class Inner { public void method() { System.out.println("method"); } } static class Inner2 { public static void print() { System.out.println("print"); } } }
Local inner classes are classes defined in methods
* Access to local variables by local internal classes must be modified with final
* Local internal classes must be final ized when accessing local variables in their methods. Why?
Because when this method is called, if the local variable is not final ized, its life cycle is the same as that of the method.
When the method stack, this local variable will disappear, so if the local internal class object has not disappeared immediately, you want to use this local variable.
No, if you use final modifier, it will go into the constant pool when the class is loaded.
Constants in the constant pool can continue to be used even if the method stack is still in use.
eg:
Anonymous inner class:class Demo1_InnerClass { public static void main(String[] args) { Outer o = new Outer(); o.method(); } } class Outer { public void method() { final int num = 10; class Inner { public void print() { System.out.println(num); } } //The method contains classes, and the domain used is the method itself. Inner i = new Inner(); i.print(); } }
The essence is an anonymous object of a subclass that inherits this class or implements the interface.
1. There is a class or interface
* The classes here can be either concrete or abstract.
2. Anonymous inner classes must have relations with some kind of interface.
3. Generally, the application is to act as a parameter in some methods.
4. Anonymous inner classes are suitable for implementing a method.
5. Format:
new class name or interface name (){
Rewriting method;
}
Such as:
new class name (){
Rewriting method;
}
Equivalent to a subclass inheriting an abstract class or a concrete class and claiming an object
new interface name (){
Rewriting method;
}
An object equivalent to a class that implements an interface.
demo :
In normal development, anonymous internals are typically passed in as method parametersclass Demo1_NoNameInnerClass { public static void main(String[] args) { Outer o = new Outer(); o.method(); } } interface Inter { public void print(); } class Outer { public void method(){ /** *If inter is an abstract class or a parent class below that *new Inter() { * public void print() { * System.out.println("print"); * } *}This block represents a subclass object that inherits inters. If an interface represents an object that implements the interface **/ new Inter() { public void print() { System.out.println("print"); } }.print();//print() implements the implementation of the inter face to produce an object and calls the print method of the object. } }
class Test1_NoNameInnerClass { public static void main(String[] args) { PersonDemo pd = new PersonDemo (); //pd.method(new Student()); pd.method(new Person() {//Anonymous inner classes are passed as parameters (essentially an anonymous inner class is treated as an object that implements or inherits Person) public void show() { System.out.println("show"); } }); } } //Both interfaces and abstract classes can abstract class Person { public abstract void show(); } class PersonDemo { public void method(Person p) { p.show(); } } class Student extends Person { public void show() { System.out.println("show"); } }
Reproduced in: https://my.oschina.net/u/580135/blog/612210