Use of anonymous and anonymous internal classes in Android

Source: Internet
Author: User

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

Related Article

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.