Inner class
General learning objectives of common classes
- Inner class
- Object class
- Common methods of Object class
- Packaging
- String class
- BigDecimal class
Inner class
classification
- Member inner class
- Static inner class
- Local inner class
- Anonymous Inner Class
concept
Define a complete class inside a class
characteristic:
-
After compilation, an independent bytecode file can be generated
-
The inner class can directly access the private members of the outer class without breaking the encapsulation
-
It can provide necessary internal functional components for external classes
Member inner class
-
Define a class within the class, which is at the same level as instance variables and instance methods
-
An instance part of an external class. When creating an internal class object, you must rely on the external class object
- Outer out = new Outer();
- Inner in = out .new Inner();
-
When the external class and internal class have the same name attribute, the internal class attribute will be accessed first
-
Member inner classes cannot define static members (can contain static quantities)
-
package com.kind.chapter_1; //head public class Body { // body private String name; class Header{ public void show(){ System.out.println(name); } } }
-
package com.kind.chapter_1; //External class public class Outer { // Instance variable private String name = "Zhang San"; private int age = 20; // Inner class class Inner{ private String address ="Beijing"; private String phone ="110"; // The attribute is the same as the attribute of the external class. If you want to access the external class name. + this. + attribute private String name = "Li Si"; private static final String country = "China"; // method public void show(){ // Print properties of external classes System.out.println(Outer.this.name); System.out.println(age); // Print the properties of the inner class System.out.println(address); System.out.println(phone); } } }
-
package com.kind.chapter_1; public class TestOuter { public static void main(String[] args) { Create an external class object // Outer outer = new Outer(); Create an internal class object // Outer.Inner inner = outer.new Inner(); // One step in place Outer.Inner inner = new Outer().new Inner(); inner.show(); } }
Static inner class
- It does not depend on external class objects, can be created directly or accessed through class names, and can declare static members
- Only static members of external classes can be accessed directly (instance members need to instantiate external class objects)
Outer.Inner inner = new Outer.Inner(); inner.show();
- The keyword static can be used only for static internal classes, and not for general classes
Local inner class
-
Defined in an external class method, the scope and creation object are limited to the current method
-
When a local internal class accesses a local variable in the current method of an external class, the variable must be modified to final because it cannot guarantee that the life cycle of the variable is the same as itself.
-
Limit the scope of use of the class: it can only be used in the current method
-
public class Outer { private String name = "Jackie Chan"; private int age = 18; public void show(){ // Define local variables String address = "Shenzhen"; // Local inner class: note that no access modifier can be added class Inner{ private String phone= "123456789"; private String email= "chenglong@qq.com"; // Cannot have static, but can add final to become a static constant (special) // private final static int count= 2000; private void show2(){ // Accessing properties of an external class System.out.println(name); System.out.println(age); //Non static can be accessed. If it becomes a static property, it needs to be accessed by a new object // Accessing internal classes System.out.println(phone); System.out.println(email); // Accessing local variables: JDK 1.7 requires that variables must be constants. final will be added automatically after 1.8 System.out.println(address); } } // Create local class object Inner inner = new Inner(); inner.show2(); } }
-
package com.kind.chapter_3; public class TestOuter { public static void main(String[] args) { Outer outer = new Outer(); outer.show(); } }
Anonymous Inner Class
-
Local inner class without class name (all features are the same as local inner class)
-
You must inherit a parent class or implement an interface
-
Define a class, implement a class, and create a syntax merge of objects. Only one object of this class can be created
-
Advantages: reduce the amount of code
-
OK: poor readability
-
package com.kind.chapter_4; public class TestUsb { public static void main(String[] args) { // Create a variable of interface type // Usb usb = new Mouse(); // usb.service(); // Local inner class // class Fan implements Usb{ // @Override // public void service() { // System.out.println("the computer is successfully connected and the fan starts to work"); // } // } // Use local inner classes to create objects // Usb usb = new Fan(); // usb.service(); // Use anonymous inner class to optimize (equivalent to creating a local inner class) Usb usb = new Usb() { @Override public void service() { System.out.println("The computer is connected successfully and the fan starts to work"); } }; usb.service(); } }
-
package com.kind.chapter_4; //Interface public interface Usb { // service void service(); }
-
package com.kind.chapter_4; public class Mouse implements Usb{ @Override public void service() { System.out.println("The computer is connected successfully, and the mouse starts to work"); } }