Anonymous inner class

Source: Internet
Author: User
/* Anonymous inner class: A class without a class name is called an anonymous inner class. The benefits of anonymous inner classes: simplify writing. The use of anonymous inner classes is premised on the existence of an inheritance or implementation relationship before it can be used. Anonymous inner classes are generally used for arguments. */abstract class Animal{public abstract Animal run ();p ublic abstract void Sleep ();} Class outer{public void print () {//requirement: Define a class within the method to inherit the animal class, and then call the Run method with sleep (). /*//local inner class dog extends animal{public void run () {System.out.println ("dog running..");} public void Sleep () {System.out.println ("dog lying in Open eyes sleeping.");}} Create object Dog d = new Dog ();d. Run ();d. Sleep (); *///Anonymous inner class: Anonymous inner classes just don't have the class name, and all the other members are. An anonymous inner class is an inherited relationship with animal. The  object that is currently creating the animal subclass. Animala = new Animal () {//  polymorphic//Anonymous internal member public Animal run () {System.out.println ("dog Running"); return this;} public void Sleep () {System.out.println ("The dog is lying awake with open eyes."); Unique method public void bite () {System.out.println ("Dog in Bite");}}; A.bite (); A.run (); A.sleep ();}} Class Demo6 {public static void main (string[] args) {//system.out.println ("Hello world!"); O uter outer = new outer (); Outer.print ();}}

  

Anonymous inner class

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.