Analysis of anonymous inner class interpretation in Java _java

Source: Internet
Author: User
Tags anonymous stub

This time looking at Android, see the Java inside the anonymous inner class, in the impression. NET does not support anonymous inner classes.

Anonymous classes are classes that cannot have names, so there is no way to reference them. They must be declared as part of the new statement when they are created. This will take another form of the new statement, as follows:
New < class or interface > < class's main >
This form of the new statement declares an anonymous class that expands on 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 extend and the interface to implement are the operands of the new statement, followed by the body of the anonymous class. If an anonymous class expands on another class, its principal can access the members of the class, the methods that override it, and so on, which is the same as any other standard class. If an anonymous class implements an interface, its principal must implement the method of the interface.

Such as:

  • Interface PR
  • {
  • void  print1 ();
  • }
  • Public class nonameclass
  • {
  • Public PR dest()
  • {
  • return New PR() {
  • Public void print1()
  • {
  • System.out.println ("Hello world!!") );
  • }
  • };
  • }
  • Public static void main (String args[])
  • {
  • Nonameclass c=new nonameclass ();
  • PR Hw=c.dest ();
  • Hw.print1 ();
  • }
  • }

PR can also be a class but the method you call outside must be declared outside of your class or interface that cannot invoke the internal method of the anonymous class

Excerpt 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 a {}
Indicates that the operation in parentheses acts on this default pair of names, and that the above Java program is followed by a function body.
The purpose of this usage is to create an instance of an object and override
A function of it. It is to Onclicklistener
An implementation of the interface.

Android Multi-Threading

 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)
    {

   &NBSP;&NBSP}

  
   };
   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.