匿名内部类小练习1


/**
 * 代码填空题
 */
public class Test16 {
    public static void main(String[] args) {
        // <使用下面这句代码输出HelloWrold>
        Outer.method().show();
    }

}

interface Inter {
    void show();
}

class Outer {
    // <补全代码>

}

========================下面是答案源码 ,一条华丽分割线===================

**
 * 匿名内部类的小练习
 */
public class Test16 {
    public static void main(String[] args) {
        // <使用下面这句代码输出HelloWrold>
        Outer.method().show();
        // 1.1 直接用类名调用方法 那么这个方法是静态的
        // 1.2 show 方法属于一个接口 我们得实现这个接口
    }

}

interface Inter {
    void show();
}

class Outer {
    // <补全代码>

    // 2.1 实现静态method方法 要调用show() method的返回值类型是Inter
    public static Inter method() {
        // 2.2 直接返回一个实现Inter类的匿名内部类 并重写内部类的show方法
        return new Inter() {
            public void show() {
                System.out.println("HelloWorld");
            }

        };
    }

}

猜你喜欢

转载自blog.csdn.net/a13085/article/details/81174308