java--匿名内部类



package com.qianfeng.demo6;

 

/**

 *  匿名内部类

 *         1、没有名字的内部类

 *         22种实现方式:实现接口      继承类

 */

public class Outer {

      

       Flyfly3 = new Fly() {

              @Override

              public void fly() {

                     System.out.println("Outer.main(...).newFly() {...}.fly()");

              }

       };

      

       publicstatic void main(String[] args) {

             

              ////Xxxx implements Fly   Fly fly = new  Xxxx();

              Fly fly2 = new FlyAnimal();

             

             

              Fly fly = new Fly() {

                     @Override

                     publicvoid fly() {

                            System.out.println("Outer.main(...).newFly() {...}.fly()");

                     }

              };

             

              fly2.fly();

              fly.fly();

             

             

              //Xxxx extends Father   Father father = new  Xxxx();

              Father father = new Father() {

                     @Override

                     publicvoid mthod() {

                           

                     }

              };

             

              Fly fly3 = new Fly() {

                     @Override

                     publicvoid fly() {

                            System.out.println("Outer.main(...).newFly() {...}.fly()");

                     }

              };

             

              Fly fly4 = new FlyAnimal();

             

             

       }

}

 

 

 

 

package com.qianfeng.demo6;

 

public class FlyAnimal implements Fly {

 

       @Override

       publicvoid fly() {

 

       }

 

}

 

 

package com.qianfeng.demo6;

 

public interface Fly {

       void  fly();

}

 

 

package com.qianfeng.demo6;

 

public class Father {

       public  void mthod(){};

}

 

 

 

你可能感兴趣的:(java--匿名内部类)