Internal learning (IV)-Anonymous internal learning

Source: Internet
Author: User

Here we will introduce a special internal class-Anonymous inner class. As the name suggests, it is an internal class without a name, this is a mechanism designed by Java to facilitate programming. Sometimes some internal classes only need to create one of its objects, and they will not be used in the future. In this case, it is more appropriate to use anonymous internal classes, it also saves the trouble of getting a name for it ,:).

What is the syntax of the anonymous class? As follows:

New supertype (){

Internal class methods and domains;

}

Note that the supertype here refers to a superclass, which can be an interface or a class. When it is an interface, there cannot be a construction parameters). The syntax of the anonymous class is a little more complex than other parts of Java. We can understand it as follows:

1. supertype is the interface

Example: interface1 () test = new interface1 (){

Method to be implemented;

Interface1 domain;

Internal class domain and method;

}

We can understand it as follows:

Declare it as the next class first,

Class anonymous1 implements interface1 {

Method to be implemented;

Interface1 domain;

Internal class domain and method;

}

Then, interface1 () test = new anonymouse1 ();

This makes it easier to understand.

2. supertype is a class

Example: class2 test = new class2 (construction parameters ){

Internal class domain and method;

}

We can understand it as follows:

Declare it as the next class first,

Class anonymous2 extends class2 {

Public anonymous2 (construction parameters ){

Super (construction parameters );

}

Internal class domain and method;

}

Then, class2 test = new anonymouse2 (construction parameters );

The test code in this section is as follows:

 

 

 

 

Package cn.edu. HUST. cm. test;

 

Import java. AWT. event. actionlistener;

Import java. AWT. event. actionevent;

Import java. AWT. toolkit;

 

Import javax. Swing. joptionpane;

Import javax. Swing. timer;

Public class innerclasstest {

Public innerclasstest (){

Super ();

}

Public static void main (string [] ARGs ){

Court court = new court (10000, true );

Court. Start ();

Joptionpane. showmessagedialog (null, "Stop? cmtober? ");

System. Exit (0 );

}

 

}

 

Class court {

Public Court (INT interval, Boolean beep ){

This. interval = interval;

This. Beep = beep;

}

Public void start (){

Actionlistener action = new actionlistener (){

Public void actionreceivmed (actionevent e ){

System. Out. println ("cindyelf, wouly you be my girl? ");

}

};

Timer T = new timer (interval, action );

T. Start ();

}

Private int interval;

Private Boolean beep;

}

 

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.