Java Learning--the use of anonymous classes

Source: Internet
Author: User

1  Public classtestproduct {2      Public Static voidMain (string[] args) {3Testproduct T =Newtestproduct ();4         //method One: Create an object that implements the class of the product interface and pass this object into the method5NoteBook n =NewNoteBook ();6 t.show (n);7         //method Two: Create an object of an anonymous class that implements the product interface8Product p =NewProduct () {9              Public voidGetName () {TenSystem.out.println ("Galaxy Note3"); One             } A              Public voidGetPrice () { -System.out.println ("¥5288"); -             } the         }; -         //method Three: Create an anonymous object that implements an anonymous class for the product interface -T.show (NewProduct () { -              Public voidGetName () { +System.out.println ("IPhone5S"); -             } +              Public voidGetPrice () { ASystem.out.println ("¥5288"); at             } -         }); - System.out.println (); -Product P1 =t.getproduct (); - p1.getname (); - P1.getprice (); in          -     } to      +     //use of local inner classes -      PublicProduct getproduct () { the //        //method One: Implement the local inner class of the product interface * //class Camera implements product{ $ //Panax Notoginseng //@Override - //Public void GetName () { the //System.out.println ("digital Camera"); + //            } A // the //@Override + //Public void GetPrice () { - //System.out.println ("4000"); $ //            } $ //             - //        } - //return new Camera (); the         //method Two: An anonymous local inner class that implements the product interface -         return NewProduct () {Wuyi @Override the              Public voidGetName () { -SYSTEM.OUT.PRINTLN ("Electric paper book"); Wu             } -  About @Override $              Public voidGetPrice () { -System.out.println ("1000"); -             } -         }; A          +     } the      -      Public voidShow (Product p) { $ p.getname (); the P.getprice (); the     } the } the  - Interfaceproduct{ in     voidgetName (); the      the     voidGetPrice (); About } the  the classNoteBookImplementsproduct{ the      Public voidGetName () { +SYSTEM.OUT.PRINTLN ("HP Notebook"); -     } the      Public voidGetPrice () {BayiSystem.out.println ("¥5000"); the     } the}

Java Learning--the use of anonymous classes

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.