Java anonymous internal class

Source: Internet
Author: User

During this time, I watched Android and saw the anonymous internal classes in Java. In my opinion,. Net does not support anonymous internal classes.

Anonymous classes cannot be referenced because they cannot have names. They must be declared as part of the new statement at creation. This requires another new statement, as shown below:
New <class or interface> <class subject>
This new statement declares a new anonymous class, which extends a given class or implements a given interface. It also creates a new instance of that class and returns it as the result of the statement. The class to be extended and the interface to be implemented are the operands of the new statement, followed by the subject of the anonymous class. If an anonymous class is extended to another class, its subject can be a member of the class, override its method, and so on, which is the same as any other standard class. If an anonymous class implements an interface, its subject must implement the interface method.

For example:

 

  • InterfacePR
  • {
  • VoidPrint1 ();
  • }
  • PublicClassNonameclass
  • {
  • PublicPR dest()
  • {
  • ReturnNewPR(){
  • PublicVoidPrint1()
  • {
  • System. Out. println ("Hello world !! ");
  • }
  • };
  • }
  • PublicStaticVoidMain (string ARGs [])
  • {
  • Nonameclass c =NewNonameclass ();
  • PR hW = C. DEST ();
  • HW. print1 ();
  • }
  • }

PR can also be a class, but the method you call externally must declare in your class or interface that the external cannot call the internal method of the anonymous class

From: http://blog.sina.com.cn/s/blog_62ea4cf40100mubj.html

Click events in Android

Button BTN = (button) This. findviewbyid (R. Id. button1 );

BTN. setonclicklistener (New button. onclicklistener (){

@ Override

Public void onclick (view v ){

// Todo auto-generated method stub

}

});

New is to create a button. onclicklistener object, followed by {}
Indicates that the operation in the brackets acts on the default object, and the above JavaProgramIs followed by a function body.
This usage is used to create an object instance and override
It is a function. It is an onclicklistener
An Implementation of the interface.

Multithreading in Android

Private runnable mrunnable = new runnable ()
{
@ Override
Public void run (){
// Todo auto-generated method stub

Try
{
Thread. Sleep (5000 );
Mhandler. sendmessage (mhandler. obtainmessage ());
}
Catch (interruptedexception E)
{

}

}
};
Public handler mhandler = new handler ()
{
Public void handlemessage (Message MSG)
{
Super. handlemessage (MSG );
Reflesh ();
}
};

 

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.