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