Package Penalty for . day27_ inner classes Anonymous inner classes; / * * anonymous inner classes: * Anonymous inner classes can be seen as a local inner class with no name * defined in the method which * he must be created in the definition of anonymous inner class time Object * format: * new new classes / interfaces () { * If you are creating a subclass object inherits this class, we can override the parent class * If you implement this interface subclass object is created, we have to realize All methods of the interface *}; * principle: instead create subclasses of objects of this class or subclass object is created to achieve this interface * * / public class InnerDemo3 { public static void main (String [] args) { Dog D = new new Dog (); d.fun (); } } class{Animal public void EAT () { System.out.println ( "eat" ); } } // normal writing class Cat the extends Animal { @Override public void EAT () { System.out.println ( "cat fish" ); } } // anonymous inner classes (no name partial internal) writing class dog { public void Fun () { / * new new Animal () { @Override public void EAT () { System.out.println ( "dog meat "); } } .eat (); */ Animal a = new Animal() { @Override public void eat() { System.out.println("狗吃肉"); } }; a.eat(); a.eat(); } }
Export
Two, new interfaces
public class InnerDemo4 { public static void main(String[] args) { Outer o = new Outer(); o.method(); } } interface Inner { public void fun(); } class Outer { public void method() { /* new Inner() { @Override public void fun() { System.out.println("i am fun"); } }.fun(); */ Inner i = new Inner() { @Override public void fun() { System.out.println("i am fun"); } }; i.fun(); i.fun(); } }
Export