匿名内部类中的接口如何工作?

编程入门 行业动态 更新时间:2024-10-24 10:27:01
本文介绍了匿名内部类中的接口如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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.

更多推荐

匿名内部类中的接口如何工作?

本文发布于:2023-11-04 05:35:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   接口   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!