InAndroidAnonymous classes (and anonymous internal classes) are often used during development. What are the differences between anonymous classes and general classes,
The functions are the same, but the use of anonymous is more concise and convenient.Point ImageNewAn interface or class, but we must
Remember that the interface is not allowed.New(Instantiate), we need to rewrite the interface method internally, so thatNewAImplementation Interface
Method object, but the class name of this object is invisible, so it is called an anonymous class. Below are the two sections with the same functionsCode, Respectively using the above two methods
Implementation:
Anonymous class
The format is as follows: New <Class or interface> <Class subject>
1 Button. setonclicklistener (listener );
2
3 Private View. onclicklistener listener = New View. onclicklistener ()
4 {
5 @ Override
6 Public Void Onclick (view V)
7 {
8 String phonenum = phoneedittext. gettext (). tostring ();
9 Intent intent = New Intent ("android. Intent. Action. Call", Uri. parse ("Tel:" + phonenum ));
10 Startactivity (intent );
11 }
12 };// A semicolon is required here.
Class of the custom inherited interface:
Button. setonclicklistener (NewButtonlistener ());
Private Final ClassButtonlistenerImplementsView. onclicklistener
{
Public VoidOnclick (view V)
{
Edittext mobiletext = (edittext) findviewbyid (R. Id. Mobile );
String mobile = mobiletext. gettext (). tostring ();
Intent intent =NewIntent ();
Intent. setaction ("android. Intent. Action. Call ");
Intent. setdata (URI. parse ("Tel:" + mobile ));
Startactivity (intent );
}
}
The anonymous class is not the same as the anonymous internal class.
The anonymous class is not the same as the anonymous internal class, which is also frequently used. The anonymous internal class is a kind of internal class, which is more concise to use, but not always used.
Thinking in Java 3th explains this:
To put it simply, an anonymous internal class is an internal class without a name. Under what circumstances do I need to use an anonymous internal class? If the following conditions are met, use the anonymous internal class for comparison.
Suitable:
· Only use one instance of the class.
· Classes are used immediately after definition.
· The class is very small (Sun recommends following four lines of code)
· Naming classes does not make your code easier to understand.
When using anonymous internal classes, remember the following principles:
· No constructor is allowed for anonymous internal classes.
· Anonymous internal classes cannot define any static members, methods, and classes.
· Anonymous internal classes cannot be public, protected, private, or static.
· Only one instance of the anonymous internal class can be created.
· An anonymous internal class must be behind New and be used to implicitly implement an interface or implement a class.
· Because the anonymous internal class is a local internal class, all restrictions on the local internal class take effect.
// Return an anonymous internal class in the Method
Public class parcel6
{
Public contents cont ()
{
Return new contents ()
{< br> private int I = 11;
Public int value ()
{
Return I;
}
}; // A semicolon is required here
}
Public static void main (string [] ARGs)
{
Parcel6 P = new parcel6 ();
Contents c = P. cont ();
}