匿名内部类中的接口如何工作? [英] How does the interface in anonymous inner class work?

查看:94
本文介绍了匿名内部类中的接口如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface MyInter {
    public void display();
}

class OuterClass8 {

    public static void main(String arg[]) {

        MyInter mi=new MyInter() {

            public void display() {
                System.out.println("this is anonymous class1");
            }
        };

        mi.display();
    }
}

据我所知,我们无法实例化界面那么这是怎么发生的呢?

As far as I know, we cannot instantiate an interface, so how did this happen?

推荐答案

你不能实例化一个接口,但你可以提供一个对象接口的引用实现接口的类,所以在代码中你实现接口并创建该类的对象并提供该类的引用。

You cannot instantiate an interface, but you can provide a reference of an interface to an object of the class implementing the interface, so in code you are implementing interface and creating an object of that class and give reference of that class.

这篇关于匿名内部类中的接口如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆