我们可以在界面中定义一个接口吗? [英] Can we define an interface within an interface?

查看:74
本文介绍了我们可以在界面中定义一个接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我们可以在界面中定义一个接口。
like

I like to know can we define an interface within an interface. like

interface abc {
    void show();
    public interface xyz {
        void read();
    }
}

这是在采访中提出的问题。任何实时使用。

This was question asked in interview. Any real time use of this.

推荐答案

是的,我们可以做到。 java中嵌套接口的定义如下:

Yes we can do it. The definition of nested interface in java is as follows:


嵌套接口是任何接口,其声明发生在另一个类或接口的主体内。顶级接口是不是嵌套接口的接口。

A nested interface is any interface whose declaration occurs within the body of another class or interface. A top-level interface is an interface that is not a nested interface.

参考以获取更多信息。

进一步...

一个原因可能是外部接口有一个将回调实现作为参数的方法。在这种情况下,嵌套接口是回调方法必须实现的契约。我没有理由在顶级声明回调接口。

One reason could be that the outer interface has a method that takes a callback implementation as argument. The nested interface is in that case the contract that the callback method must implement. I don't see a reason to declare that callback interface at top level.

public interface Processor {
   void execute(NotificationListener listener);

    interface NotificationListener {
        void processingCompleted();
    }  }

太阳网站关于此主题的另一个好读物是此处

Another good reading at sun site about this topic is here

特别注意何时你实现了一个接口,你不需要
来实现嵌套在其中的任何
接口。

In particular, notice that when you implement an interface, you are not required to implement any interfaces nested within.

这篇关于我们可以在界面中定义一个接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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