catalogue
What is an anonymous inner class?
concept
🔥🔥🔥 Today, I will explain the inner classes and anonymous inner classes to you. I hope they can help you in the process of learning Java.
- An internal class is a class defined in a class. The internal class can be understood as (parasitic), and the external class can be understood as (host)
public class People{ //Inner class public class love{ } }
📗 Usage scenarios and functions of internal classes:
- When an internal part of a thing needs a complete structure to describe, and the internal complete structure only provides services for external things, the entire internal complete structure can be designed using internal classes.
- Internal classes usually provide easy access to members of external classes, including private members.
- The inner class provides better encapsulation. The inner class itself can be decorated with private protectecd, and encapsulation can be more controlled.
🗞️ Classification of internal classes:
Static inner class, member inner class (non static inner class), local inner class, anonymous inner class
What is a static inner class?
- It is decorated with static and belongs to the external class itself
- Its characteristics and use are exactly the same as those of ordinary classes. It has all the components of classes, but it is located in others
public class outer{ //Static inner class public class static inner{ } }
📘 Format of static internal class creation object:
External class name Internal class name object name = new external class name Internal class name ();
outer.inner in = new outer.inner;
public class computer { public static String name = "Rimi Ishihara"; public static class mouse{ private String name ; private int price; public mouse() { } public mouse(String name, int price) { this.name = name; this.price = price; } public void buy(){ System.out.println(computer.name+"Purchased"+this.name); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } } } //main method computer.mouse m = new computer.mouse("Thunder snake",300); m.buy(); //Output results: //Rimi Ishihara bought thunder snake
The internal class can access the static members of the external class, but cannot access the instance members of the external class. Because the instance members are created by objects, the internal class cannot be found, unless an object is created in the internal class for indirect access.
What is a member inner class?
Objects belonging to external classes without static decoration
//format public class outer{ public class inner{ } }
📙 Format of object created by member inner class:
External class name Internal class name object name = new external constructor New internal constructor ();
outer.inner in = new outer().new inner();
public class computer { public static String name1 = "Rimi Ishihara"; private String name2 ; public class mouse{ private String name ; private int price; public void buy(){ System.out.println(computer.name1+"Purchased"+this.name); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } } } //main method computer.mouse m = new computer().new mouse(); m.setName("Thunder snake"); m.buy(); //Output results: //Rimi Ishihara bought thunder snake
Compared with the static internal class, the member internal class can access not only the static member variables of the external class, but also the instance members of the external class. The reason is that the instance object of the internal class is created after the external class is created. It can directly call the instance members of the previously created object.
What is a local inner class?
Local inner classes are placed in methods, code blocks, constructors, and other executors
The class file name of the local internal class is: external class $N internal class class
Format (defined in the main method):
Class class name{
}
Just for understanding
What is an anonymous inner class?
It is essentially a local inner class without a name, which is defined in methods, code blocks, etc.
Function: it is convenient to create subclass objects. The ultimate purpose is to simplify code writing.
📒 Format:
new class name | abstract class name | or interface name (){
Rewrite method;
};
People p = new People(){ public void run(){ } }; p.run();
We have learned about polymorphism before. We rewrite the methods of subclasses and then re access the methods in subclasses. Here is the difference between using anonymous inner classes and not using them:
Past methods:
public static void main(String[] args) { //Parent class class People{ void run() { } } //Subclass class Student extends People{ @Override public void run() { System.out.println("Pupils run slowly"); } } People p = new Student(); p.run(); } //Output results: //Pupils run slowly
After using anonymous inner classes:
public static void main(String[] args) { class People{ void run() { } } //Anonymous Inner Class People p = new People(){ void run(){ System.out.println("Pupils run slowly"); } }; p.run(); } //Output results: //Pupils run slowly
characteristic:
- An anonymous inner class is an inner class without a name
- When an anonymous inner class is written, an object of an anonymous inner class will be generated
Compared with before, the main difference is that you can rewrite methods without instantiating objects, and save a certain amount of code space.
It's not easy to create. Let's give it to the third company