Java---Anonymous class summary

Source: Internet
Author: User

Learning Android today, one way to implement the OnClick event for a button control is to use an anonymous class, where anonymous classes in Java are summarized.

Anonymous inner class--an inner class without a name, because it has no name, so it can only be used once

Anonymous inner class--it is often used to simplify code writing

Use anonymous inner class prerequisites: You must inherit from a parent class or implement an interface

Note: As long as a class is abstract or an interface, the methods in its subclasses can be implemented using anonymous inner classes;

The most common situation is in the implementation of multithreading , because to implement multithreading must inherit the thread class or inherit the Runnable interface

1: Examples of implementations that do not use anonymous classes

1 Abstract classperson{2     Abstract voideat ();3 }4 5 classStudentextendsperson{6     voideat () {7System.out.println ("Student eat!");8     }9 }Ten  Public classInnerclass { One      Public Static voidMain (string[] args) { APerson Stu =NewStudent (); - stu.eat (); -     } the  -}

Output Result: >student eat!

Implementation process: The student class inherits the person class, implements an instance as its subclass, and translates it upward into a reference to the person class.

2: Anonymous inner class basic implementation mode

1 Abstract classperson{2      Public Abstract voideat ();3 }4 5  Public classInnerclass {6      Public Static voidMain (string[] args) {7Person Stu =NewPerson () {8              Public voideat () {9System.out.println ("Student eat!");Ten             } One         }; A stu.eat (); -     } -  the}

Output Result: >student eat!

Implementation: Put the abstract method eat () in the person class directly into the curly braces, note that there is a semicolon after the curly braces!

3: Anonymous inner class used on interface

1 Interfaceperson{2      Public Abstract voideat ();3 }4 5  Public classInnerclass {6      Public Static voidMain (string[] args) {7Person Stu =NewPerson () {8              Public voideat () {9System.out.println ("Student eat!");Ten             } One         }; A stu.eat (); -     } -  the}

Output Result: >student eat!

4: Implementation of anonymous classes in thread threading class

(1) Implementation of the Run () method in the Thread class

1  Public classInnerclass {2      Public Static voidMain (string[] args) {3Thread T =NewThread () {4              Public voidrun () {5                  for(inti = 0; I < 5; i++){6System.out.println ("x =" + i + ";"));7                 }8             }9         };Ten T.start (); One     } A  -}

Output Result:

x = 0;
x = 1;
x = 2;
x = 3;
x = 4;

(2) Implementation using the Runnable interface

1  Public classInnerclass {2      Public Static voidMain (string[] args) {3Runnable r =NewRunnable () {4              Public voidrun () {5                  for(inti = 0; I < 5; i++){6System.out.println ("x =" + i + ";"));7                 }8             }9         };Ten         Thread t = new  thread (r);  One T.run (); A     } -  -}

The result is the same as above, and the thread and the runnable are connected by the instance R of Runnable.

5: Extension: In Android Development button Implementation Onclicklistener () event using anonymous inner class implementation The main code is as follows:

1 /*2 * Initialize button control;3 * Set button listener to implement click action through Listener4          */5Loginbutton =(Button) Findviewbyid (r.id.button1);6Loginbutton.setonclicklistener (NewOnclicklistener () {7              Public voidOnClick (View args0) {8SYSTEM.OUT.PRINTLN ("Welcome!" ");9             }Ten});

Note that the implementation of the entire Onclicklistener () event is contained in the Setonclicklistener () method

Java---Anonymous class summary

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.