Anonymous inner class interpretation analysis in Java

  • 2020-04-01 01:47:21
  • OfStack

This time in the android, saw the anonymous inner class in Java, in the impression that.net does not support anonymous inner class.

Anonymous classes are classes that cannot have names, so there is no way to reference them. They must be declared as part of a new statement when created. This takes another form of the new statement, as follows:
New < Class or interface > < The body of the class >
This form of new statement declares a new anonymous class that extends a given class or implements a given interface. It also creates a new instance of that class and returns it as a 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 body of the anonymous class. If an anonymous class extends another class, its principal can access its members, overwrite its methods, and so on, just like any other standard class. If an anonymous class implements an interface, its body must implement the interface's methods.

Such as:

 

interface pr {          void Print1 ();   }   Public  The class NoNameClass   {   public Pr dest ()     {            Return    The new Pr () {                    Public  void Print1 ()                   {                       System. Out.println (" Hello world!!!!!" );                   }         }; }   Public      Static      void The main (String args []) {              NoNameClass c = The new NoNameClass ();             Pr hw = c.d est ();             Hw. Print1 ();       }   }  

Pr can also be a class but methods that you call externally must declare in your class or interface that the external cannot call methods inside anonymous classes

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

Click events in android

This Button BTN = (Button). The findViewById (R.i db utton1);    

BTN. SetOnClickListener (new Button. An OnClickListener () {

      @ Override      

    Public void onClick(View v) {     

                  // TODO auto-generated method stub & zip;      

          }          

  });

  New is to create a button.onclicklistener object, followed by a {}
Indicates that the action in the parenthesis applies to the default namespace, followed by a function body in the Java program above.
This usage creates an instance of the object and override
A function of that. It is for an OnClickListener
An implementation of the interface.

Android multithreading

  Private Runnable mRunnable = new Runnable()
      {
    @ Override
    Public void the run () {
      // TODO auto-generated method stub

        The 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 ();
        }
      };


Related articles: