【重点】匿名内部类

/*按照要求,补齐代码
interface Inter {
void show();
}
class Outer {
//补齐代码
}
class OuterDemo {
public static void main(String[] args) {
Outer.method().show();
}
}

引用块内容
要求在控制台输出”HelloWorld”。*/
Outer.method()方法调用语句,说明method()方法是Outer类中的一个静态方法,先在代码上写下这个method方法。前面的Outer.method()的类型是Inter类型的(因为调用了show()方法),也就是接口类型的,method()方法应该有一个Inter类型的返回值,返回给Inter类型的子类对象。最后【重点】在method方法里面写一个匿名内部类并将它作为method方法的返回值。

package myInterfaceDemo;
/* Outer.method().show();*/
class Outer {
Outer outer=new Outer();

//补齐代码
    public static Inter method(){
        Inter inter=new Inter() {
            @Override
            public void show() {
                System.out.println("helloworld");
            }
        };
        return inter;
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_39530338/article/details/82048483