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