Java learning - anonymous inner class

/ *
If the interface implementation class (or parent subclass) only need to use only once,
so in this case you can omit the class definition, but instead use anonymous inner classes [].

Anonymous inner classes defined format:
Interface Name Interface name = new object name () {
// abstract covers all methods rewriting
};

The format of the "new interface name () {...}" parsing:

  1. new representatives to create an object action
  2. Which interface is the interface name anonymous inner class needs to implement
  3. {...} This is the contents of the anonymous inner classes

Also note a few points:

  1. Anonymous inner class, create an object when [], use only the only time.
    If you want to create an object several times, but the same kind of content, then you need to use a separate implementation class defined in the.
  2. Anonymous objects, invoke methods in [time], can only be called only time.
    If you want the same object, call the method multiple times, you must give the object a name.
  3. Anonymous inner classes are omitted [implementation class / subclass name], but [anonymous object is an object name omitted]
    stressed: anonymous inner classes and anonymous object is not the same thing! ! !
    * /
public class DemoMain {

    public static void main(String[] args) {
//        MyInterface obj = new MyInterfaceImpl();
//        obj.method();

//        MyInterface some = new MyInterface(); // 错误写法!

        // 使用匿名内部类,但不是匿名对象,对象名称就叫objA
        MyInterface objA = new MyInterface() {
            @Override
            public void method1() {
                System.out.println("匿名内部类实现了方法!111-A");
            }

            @Override
            public void method2() {
                System.out.println("匿名内部类实现了方法!222-A");
            }
        };
        objA.method1();
        objA.method2();
        System.out.println("=================");

        // 使用了匿名内部类,而且省略了对象名称,也是匿名对象
        new MyInterface() {
            @Override
            public void method1() {
                System.out.println("匿名内部类实现了方法!111-B");
            }

            @Override
            public void method2() {
                System.out.println("匿名内部类实现了方法!222-B");
            }
        }.method1();
        // 因为匿名对象无法调用第二次方法,所以需要再创建一个匿名内部类的匿名对象
        new MyInterface() {
            @Override
            public void method1() {
                System.out.println("匿名内部类实现了方法!111-B");
            }

            @Override
            public void method2() {
                System.out.println("匿名内部类实现了方法!222-B");
            }
        }.method2();
    }

}

public interface MyInterface {

    void method1(); // 抽象方法

    void method2();

}
public class MyInterfaceImpl implements MyInterface {
    @Override
    public void method1() {
        System.out.println("实现类覆盖重写了方法!111");
    }

    @Override
    public void method2() {
        System.out.println("实现类覆盖重写了方法!222");
    }
}
Published 55 original articles · won praise 0 · Views 385

Guess you like

Origin blog.csdn.net/qq_44813352/article/details/104365888