Internal class, local internal class, anonymous internal class

Source: Internet
Author: User
Tags date now
++ ++ ++
Internal class, local internal class, anonymous internal class
++ ++ ++

/**
* <Internal class>: class defined in another class.
* Usage reason:
1. Internal class method, which can access the data in the scope of the <external class> definition, including private data
2. The internal class can be hidden from <other classes in the same package>
3. When you want to define a <callback function> and do not want to write a lot of code, use the <Anonymous class> simple
**/
Public class talkingclock {

Private int interval;
Private Boolean beep;

Public void start (){
...
}

// Internal class
Public class timeprinter implements actionlistener {
@ Override
Public void actionreceivmed (actionevent event ){
Date Now = new date ();
...
// Outerclass. This. ATTR ---> use <external class> reference in <internal class>
If (talkclock. This. Beep) Toolkit. getdefatooltoolkit (). Beep ();
}
}
}

/**
* <Local internal class> ----- define <internal class> in a method of <external class>
**/
Public void start (){

// 1. The local class cannot be declared using public/private, and its effect is limited to the <block> of the local class.
// 2. Local class [advantage]: completely hidden from <block>, even if <external class> other Code cannot be accessed
Class timeprinter implements actionlistener {
@ Override
Public void actionreceivmed (actionevent event ){
Date Now = new date ();
...
}
}

Actionlistener listener = new timeprinter ();
}


/**
* <Anonymous internal class> (Anonymous class) ------ no class name; therefore, no constructor is allowed;
* <Anonymous internal class> is an in-depth step towards <partial internal class>. If you only create one object for this class, you do not need to name it.
**/
Public void start (INT interval, final Boolean beep ){

Actionlisten listener = new actionlistent (){
@ Override
Public void actionreceivmed (actionevent event ){
Date Now = new date ();
....
}
}
}

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.