Anonymous internal class, anonymous

Source: Internet
Author: User

Anonymous internal class, anonymous

Anonymous internal class, similar to the anonymous object we have learned before...

Anonymous internal classes are simplified Writing of internal classes. The premise is that there is a class or interface. The classes here can be specific classes or abstract classes...

Format: new Class Name or Interface Name () {rewrite method ;}

What is the essence?
Is an anonymous object that inherits this class or implements this interface...

The format of the anonymous internal class is as follows...

Public class Test {
Public static void main (String [] args ){
Outer o = new Outer ();
O. method ();
}
}
Interface Inter {
Public void print ();
}
Class Outer {
Class Inner implements Inter {
Public void print (){
System. out. println ("print ");
}
}
Public void method (){
Inner I = new Inner ();
I. print ();
}
}

Note: The anonymous internal class is a type of local internal class. Since it is a type of local internal class, the anonymous internal class must be located in the member method...

Inner is a member internal class of the Outer class. The Inner class implements the interface Inter. The Inner class has a print method, and the Outer class also has a member method. As mentioned above, the premise of an anonymous internal class is that there is a class or interface. Of course, this class can be either a specific class or an abstract class. Let's write the code of an anonymous internal class again.

First look at the format of the anonymous internal class: new Class Name or Interface Name () {rewrite method ;}

We have learned abstract classes before and further learned interfaces. interfaces are the product of full abstraction. member variables in interfaces can only be constants, the member methods can only be abstract methods. As mentioned above, interfaces are not a class, but interfaces have many similarities with classes. In a sense, we can also regard the interface as a class to understand, class inheritance Class, Class implementation interface, interface inheritance interface. When a class is used to implement an interface, we can also regard this interface as a "Special Class". Now we need to write an anonymous internal class code, so we should first write an interface as required, write code in Editplus...
The premise is that there is an interface or a class. First, start with the interface and write an interface... the method in the interface does not have a method body and is an abstract method. Since the anonymous internal class is a local internal class, we need to write an external class...

Public class Test {
Public static void main (String [] args ){

}
}

Interface Inter {
Public void print ();

}

Class Outer {

}

First, build the code framework. First, we define an interface, which has a print method. Since the interface is defined, our goal is to use the print method in the interface. However, to use the print method in the interface, we cannot directly use this print method. We must create a class to implement this print method. At this time, we can think that this class inherits this "Interface Class". Because the "Interface Class" contains abstract methods, you must override the methods in the interface to implement interfaces in common classes, so what kind of common class should be created to override the interface method? Here, we learn the Anonymous class, because it should be thought that the Anonymous class will come in handy...

Public class Test {
Public static void main (String [] args ){

}
}

Interface Inter {
Public void print ();

}

Class Outer {
Public void method (){
Class Inner implements Inter {
Public void print (){
System. out. println ("print ");
}
}
}

}

In this way, the method acts as a Member method of the Outer class, and there is an Inner class in the method to implement the Inter interface. Now my goal is to call the print method. Obviously, there are two levels. The Inner class objects call the print method, then, the external class Outer calls the internal class Inner, but the method called by the object of the external class Outer class does not mean that the Inner object is called. What should we do ???

Inner I = new Inner ();
I. print ();

Outer o = new Outer ();
O. method ();

Because the Outer class only has one unique method, only the method can be called directly in the Outer class. As mentioned above, calling a method not only assigns values to the form parameter, but also executes the method statement, the returned value is also obtained. Since the Outer class can only call the method, to ensure that the Inner class object can call the print method while calling the method, we encapsulate the statements for creating an object in the Inner class and calling the print method into the method of the Outer class, so that we can achieve a one-time call, you can go through two levels...

Therefore, the complete code is

Public class Test {
Public static void main (String [] args ){

Outer o = new Outer ();
O. method ();

}
}

Interface Inter {
Public void print ();

}

Class Outer {
Public void method (){
Class Inner implements Inter {
Public void print (){
System. out. println ("print ");
}
}
Inner I = new Inner ();
I. print ();
}

}

However, even though the print method has been called in the above code, I found that the code I wrote has nothing to do with the anonymous internal class. So, what exactly, can I use an anonymous internal class ???

Let's take a look at the code of an anonymous internal class.

Class Demo1_NoNameInnerClass {
Public static void main (String [] args ){
Outer o = new Outer ();
O. method ();
}
}

Interface Inter {
Public void print ();
}

Class Outer {
Class Inner implements Inter {
Public void print (){
System. out. println ("print ");
}
}
Public void method (){
New Inter (){
Public void print (){
System. out. println ("print ");
}
}. Print ();
}
}

In fact, we can see this piece of code. Why? Because our ultimate goal is to call the print method, let's look at the previous code.


Public class Test {
Public static void main (String [] args ){

Outer o = new Outer ();
O. method ();

}
}

Interface Inter {
Public void print ();

}

Class Outer {
Public void method (){
Class Inner implements Inter {
Public void print (){
System. out. println ("print ");
}
}
Inner I = new Inner ();
I. print ();
}

}

Since the anonymous internal class is a local internal class, we should first create a method and write an anonymous internal class in this method according to the standard format... and define an interface...

Let's look at the following framework.

Class Demo1_NoNameInnerClass {
Public static void main (String [] args ){
Outer o = new Outer ();
O. method ();
}
}

Interface Inter {
Public void print ();
}

Class Outer {

Public void method (){
New Class Name or Interface Name () {rewrite method ;}
}
}

Here, the interface Inter is defined and the method is defined in the Outer class, and then you can see what content is entered in the method. The difficulty here is, new Class Name or Interface Name () {rewrite method;} the class name or interface name here, which class or interface should be filled in? Since an interface is defined here, so we must have some purpose, because the interface has the meaning that there is a class to implement this interface and rewrite the method of this interface. Why should we rewrite the method of this interface, of course, we need to use this method to rewrite it. Otherwise, the rewriting is of little significance. Therefore, a class is missing, and this class is required to implement the Inter interface. Therefore, see the following code.

Class Inner implements Inter {
Public void print (){

System. out. println ("print ");
}
}

Then, consider where to place the above Code. If the Inner and Outer are at the same level, as shown below:

Class Test {
Public static void main (String [] args ){
Inner I = new Inner ();
I. print ();
}
}

Interface Inter {
Public void print ();
}
Class Inner implements Inter {
Public void print (){

System. out. println ("print ");
}
}

Class Outer {

Public void method (){

}
}

At this time, it is basically not related to the anonymous internal class, because if the Outer class and Inner class are at the same level, the Outer class code is completely redundant at this time, and this Code is cut off, the program can run as follows:

Class Test {
Public static void main (String [] args ){
Inner I = new Inner ();
I. print ();
}
}

Interface Inter {
Public void print ();
}
Class Inner implements Inter {
Public void print (){

System. out. println ("print ");
}
}

Therefore, the correct relationship between the Inner class and the Outer class should be that the Outer class contains the Inner class, that is, the Inner class is an internal class, the emergence of anonymous internal classes makes more sense... So, let's look at the following code. Although the following is only a piece of incomplete and nonstandard code, it provides inspiration for us to write the correct format.

Class Test {
Public static void main (String [] args ){
Inner I = new Inner ();
I. print ();
}
}

Interface Inter {
Public void print ();
}


Class Outer {
Class Inner implements Inter {
Public void print (){

System. out. println ("print ");
}
}
Public void method (){

}
}

In this case, to call the print method, the Inner class object needs to call the print method. However, to trigger the Inner class, the object of the external class needs to call the internal class. Therefore, back to the first place where we learned internal classes, but if you follow the initial format, see the following

Class Test {
Public static void main (String [] args ){
Outer. Inner I = new Outer (). new Inner ();
I. print ();
}
}

Interface Inter {
Public void print ();
}


Class Outer {
Class Inner implements Inter {
Public void print (){

System. out. println ("print ");
}
}
Public void method (){

}
}

This code can also be compiled and run successfully, but in this way, I found that the method is redundant and the anonymous internal classes do not appear, so to change this pattern, you must

Outer. Inner I = new Outer (). new Inner ();
I. print ();

Starting with the above two sentences of code, of course, here, I. print (); indicates that the print method is called directly. Although my goal is to use the print method or call the print method, I can not directly call the print method, call another method and encapsulate the statement that calls the print method directly in the method body. As I have said before, calling a method is equivalent to porting code, in this way, the print method is indirectly called by calling the method. Let's look at the following code:

Class Test {
Public static void main (String [] args ){
Outer o = new Outer ();
O. method ();
}
}

Interface Inter {
Public void print ();
}


Class Outer {
Class Inner implements Inter {
Public void print (){

System. out. println ("print ");
}
}
Public void method (){
Inner I = new Inner ();
I. print ();

}
}

The code functions are the same. The method is called through the Outer class object to call the print method. However, the anonymous internal class is still not used, how can an anonymous internal class be used ??????

We have previously learned the new class name (). This format represents an object. Therefore, let's look at the format of anonymous internal classes.

New Class Name or Interface Name () {rewrite method ;}

This looks like an object format. However, for the above Code, which of the class names and interface names in this format is Outer Inner or Inter, what is hard to understand here is that the premise of an anonymous internal class is that there is a class or an interface. You do not need to go into the premise that there is a class or an interface, because this is a rule in java, if we want to use java, we must follow its rules. We should not worry about the premise first. First, we should study the anonymous internal class, which is actually an anonymous class located in the member method, the anonymous class can be compared to the previously learned anonymous objects. However, if you want to define an anonymous class, it is hard to figure out the syntax structure of the anonymous class. However, in java, you do not need to directly use the class keyword to define an anonymous class. Usually, when you define an anonymous class, an anonymous class object is generated. The format of the object has been learned before, new Class Name (); this format represents an object, but the anonymous internal class depends on the interface or parent class, so, creating an anonymous internal class object is bound to an interface or parent class. If you use an interface to implement an anonymous internal class object or create an anonymous internal class object is related to an interface, here, we don't have to worry about the syntax structure. The above Code has already defined the interface Inter. So, the purpose of writing the code is to achieve what we want to do, my goal is to call the print method. As I mentioned earlier, the method is called through the Outer class object to indirectly call the print method. According to the code porting theory, the Code that directly calls the print method must be encapsulated in the method. Who can directly call the print method is of course an object with the print method. For example, a class defines the print method, therefore, this class and the object generated by this class have the print function. Therefore, this object can call the print method, now we use an anonymous class object to directly call this print method. This Anonymous class has no name and I don't know how to express it, because the Anonymous class has no name, how do you express it? However, I know the format of the anonymous class object and it is easy to know the format, because the interface Inter has been defined, this Inter is used as the premise to represent an anonymous class object.

New Inter (){}

Although this anonymous class object is represented, this object has no name and only one representation. It is called anonymous because there is no name. At this time, we have created this anonymous class object, however, I do not know the morphological characteristics of this object. The member variables and member methods in this object are not directly described. At this time, the key point is here. As I have said before, an interface can be understood as a special class. The Inheritance and implementation are similar, so the Inter "Interface Class", when an anonymous class object is generated, first, this anonymous class can be considered to inherit this special "Interface Class". Therefore, this class inherits the interface class, and its method comes from the interface, but the method in the interface must be rewritten to make sense, then rewrite the interface method and complete the code

New Inter () {public void print () {System. out. println (" ");}}

At this time, we have completely seen through this anonymous class object, in fact, it is to rewrite the interface method, the other is the same as the interface, that is, this anonymous class object has a print method, in other words, since this anonymous class object has the print function, this anonymous class object can certainly call the print method.

New Inter () {public void print () {System. out. println ("") ;}}. print ()

The print function of this anonymous class object is to print out your sister

Complete code:

Class Test {
Public static void main (String [] args ){
Outer o = new Outer ();
O. method ();
}
}

Interface Inter {
Public void print ();
}

Class Outer {


Public void method (){



New Inter () {public void print () {System. out. println ("") ;}}. print ();

}
}

As long as you know, the anonymous internal class is actually only an anonymous class, but its location is in the method...

However, the anonymous internal class is only used to override one method...

Application of anonymous internal classes in Development (passed as parameters)

See the following code:

Class Test {
Public static void main (String [] args ){

PersonDemo pd = new PersonDemo ();

Pd. method (new Person (){
Public void show (){
System. out. println ("show ");
}
});
}
}

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 ");
}
}

As I said above, anonymous internal classes are always accompanied by classes or interfaces, and anonymous internal classes must rewrite methods. This shows that anonymous internal classes are related to abstract classes, because rewriting is generally related to abstract methods, we can use abstract classes or abstract interfaces to say that...
Write an abstract class. Classes containing abstract methods must be abstract classes...

Abstract class Person {
Public abstract void show ();
}


Write another class.
Class PersonDemo {
Public void method (Person p) {p. show ();}

}

This class contains a method. The parameter of the method is an object of the Person class. In fact, Person p and int
This format is similar. we can regard Person as a data type, and p as the variable name. However, int a here, a is of the int type, that is, the basic data type, p is a reference data type, so it can also be referred to as the reference p of the Person class. We usually say the object p of the Person class. In the method statement of the method, is to use object p to call the show method... here, the function parameter is the reference variable p of the Person class. We have learned that the function parameter can be of the basic data type or reference data type...

Write a class Student as a subclass of the Person class...

Class Student extends Person {
Public void show (){
System. out. println ("show ");
}

}

Now we notice that in the PersonDemo class, in the method, this method parameter is the object of the Person class. Now I need to call this method to see how to call it... if you pass an anonymous internal class object as a parameter to the method

PersonDemo pd = new PersonDemo ();
Pd. method (anonymous internal Class Object );

Now, we should try to figure out how to represent this anonymous internal class object. Previously, the anonymous internal class is actually an anonymous class located in the member method. Therefore, we should study the characteristics of the anonymous class. Because the Anonymous class has no name, it cannot be defined in a common format. For example, class Test {} is a Test class, generally, we do not define an anonymous class separately. Because we define an anonymous class separately, there is no format to express it. Generally, we define an object of an anonymous class, that is, the definition of an anonymous class is actually implemented by defining an anonymous internal class object. Previously, we learned that the new class name () is an object. Therefore, here, we also use the new keyword to represent an anonymous internal class object. Because the anonymous internal class is associated with the rewrite method, we must find a class or interface, as the parent class or father of this anonymous class, that is to say, the generation of anonymous classes is not generated out of thin air. A class or interface must be used as the parent class and inherit the features of the parent class, this is also the basis for the generation of anonymous classes...

So here, we select the abstract class Person as the parent class of the anonymous class. That is to say, the abstract class Person is the basis of the anonymous class, and the format is displayed.

New Person () {public void show ()

{System. out. println ("show ");}}
Then pass the Anonymous class object as a parameter to the method...

The complete code is

Class Test {
Public static void main (String [] args ){

PersonDemo pd = new PersonDemo ();

Pd. method (new Person () {public void show ()

{System. out. println ("show ");}});
}
}


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 your sister ");
}

}

 

Note Analysis

New Person () {public void show ()

{System. out. println ("show ");}}

This statement is an anonymous Class Object. This object has no name. new Person () indicates that this anonymous class uses the Person class as the parent class, or the Person class is the basis for the generation of this anonymous class, then, in addition to rewriting the show method of the parent class Person class, this anonymous class has the same features as the parent class. That is to say, the characteristics of the anonymous class are inherited from the parent class or the interface, when an anonymous Class Object calls the show method, the call method is code porting, that is, the show method function of the anonymous class object is used, so the show string is printed...

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.