preface
The anonymous inner class of Java is the first important and difficult point for me to get started with Java. I'll come and summarize it just after class.
Because the examples given by the teacher in class are vivid and interesting, I will imitate and reproduce them here: first, there is a class called animal, then there is a subclass bird in this class, and then an object sparrow is exemplified in birds. The animal method is to output "all animals will shout ~" while the bird method is to output "chirp!". Now all we have to do is let the sparrows "chirp!"
The following three programs will be posted. We can realize the three concepts of polymorphism, upward transformation and anonymous internal class.
1, The most intuitive way to write -- override the parent method (polymorphic)
We use an example of animal cry to discuss the problem. First, complete the program code in normal writing:
public class AnimalSound { public static void main(String[] args) { // Method stub automatically generated by TODO Bird sparrow = new Bird(); //Define subclass objects and instantiate them sparrow.sound(); } } class Animal{ void sound() { System.out.println("All animals make sounds~"); } } class Bird extends Animal{ void sound() { //Override the method of the parent class System.out.println("Twitter!"); } }
Correct output:
Twitter!
Now, let's take a look at this program: first, it is divided into three classes. The first is the main class, the second is the ordinary class Animal, and the third is the subclass Bird inherited from Animal; Then, the Bird class is instantiated in the main class, and a sparrow object appears. Finally, the object's method sound is used. In fact, this program embodies one of the three characteristics of object-oriented: polymorphism. What do you say? Just look at the subclass Bird. Here, we override the method of the parent class in the subclass. Later, after using this method in the main class, the output of the subclass method instead of the parent class will be displayed. Ha ha.
2, Transitional writing - internal class + upward transformation
Upward transformation means assigning a subclass object to a parent object:
public class AnimalSound { public static void main(String[] args) { // Method stub automatically generated by TODO class Bird extends Animal{ //Inner class void sound() { System.out.println("Twitter!"); } } Animal sparrow = new Bird(); //Upward transformation sparrow.sound(); } } class Animal{ void sound() { System.out.println("All animals make sounds~"); } }
Correct output:
Twitter!
Different from the previous code, this subclass is written in the main class, so it is an internal class. Later, when the instance is declared, it is written like this - Animal sparrow = new Bird();, The left side of the equal sign is to define a parent object, the right side of the equal sign is to instantiate a subclass, and the equal sign is actually an assignment operation. Think about it. When a subclass is instantiated and assigned to its parent object, the son runs up to find his father. This is upward transformation.
In addition, let's talk about the problem of methods in objects generated after upward transformation. Do methods use parent or child classes? It depends on the methods of the subclass and the parent. The subclass is smart. If it's me, I don't want to change my mobile phone with my father, ha ha). If it's not me, I'll directly use the parent (just like I charge my father's living expenses every month, I don't have money, manual dog head). In this way, it should be much clearer.
3, Final writing - anonymous inner class
In fact, it is the final writing method. In fact, it is the most common writing method of Java. It is just that we have just come into contact with Java and are still unfamiliar with object-oriented programming.
public class AnimalSound { public static void main(String[] args) { // Method stub automatically generated by TODO Animal sparrow = new Animal() { //Gets an instance of an anonymous inner class object void sound() { //Override the method of the parent class System.out.println("Twitter!"); super.sound(); //Call the sound method of the parent class } //(I just want to remember the usage of super) }; //Upward transformation sparrow.sound(); //Call the sound method of the anonymous class } } class Animal{ void sound() { System.out.println("All animals make sounds~"); } }
Correct output:
Twitter! All animals make sounds~
See, in this program, you can't find the name of the subclass. Who knows what its name is, but it can still instance an object spark: Animal spark = new animal (). A parent object is defined on the left of the equal sign, and a parent class is instantiated and valued on the right of the equal sign. That is to say, after skipping the bird subclass from an animal class and directly instantiating the sparrow object, the following operations are available (overriding the parent class method and executing it).
summary
Anonymous. Since the subclass name does not appear, it is impossible to find it again. Therefore, it is very convenient to use anonymous inner classes for classes that appear only once. Finally, a more concise way of anonymous inner class code is given:
public class AnimalSound { public static void main(String[] args) { // Method stub automatically generated by TODO new Animal() { void sound() { System.out.println("Twitter!"); } }.sound(); //Upward transformation } } class Animal{ void sound() { System.out.println("All animals make sounds~"); } }
Correct output:
Twitter!
Is there a direct wow like me? This time, even the definition is omitted. Directly instance an object, and then take the whole code block (this object) as the name of the object and directly call the method. It's awesome! But think about it carefully. The essence of asking Zhang San to sweep the floor is to ask this specific person to sweep the floor. Therefore, it's good to give Zhang San anonymity. Whether you are Zhang San or Li Si, Ta's job is just sweeping the floor. Just sweep it well.