简体   繁体   English

类内部接口

[英]Inner Interface inside a Class

My code is as following, my question is that what is the meaning of line 1 , if there is not meaning then why does eclipse allows infact gives inner as available option on typing ClassO. 我的代码如下,我的问题是第1行含义是什么,如果没有意义那么为什么eclipse允许infact在输入ClassO时给内部提供可选的选项。

It would have been meaningful if the inner was a static nested class instead of inner interface and if it meaningful as it is then what is the use, what all can i do with it ? 如果内部是一个静态嵌套类而不是内部接口, 如果它有意义 ,那么它有什么用处,那么我可以用它做什么呢?

public class InterfaceInsideClass 
{

    public static void main(String[] args) 
    {
        ClassO.inner              **// Line 1**
    }

}

class ClassO
{
    interface inner
    {
        void msg();
    }

    class Two implements inner
    {
        public void msg()
        {
            System.out.println("Class Two");
        }
    }

    class Three
    {
        public void msg()
        {
            System.out.println("Class Three not implementing interface but having same method");
        }
    }

    static class Four
    {
        public void msg()
        {
            System.out.println("Class Four");
        }
    }
}

One on the most common use of static nested classes and nested interface is for another layer of packaging. 静态嵌套类和嵌套接口最常见的用途是另一层封装。

If the particular interface is only intended to be implemented within a particular class or its subclasses via by its inner or nested classes then it makes sense to include that interface inside those classes. 如果特定接口仅用于通过其内部或嵌套类在特定类或其子类中实现,那么将该接口包含在这些类中是有意义的。

Good example is: java.util.Map.Entry . 好的例子是: java.util.Map.Entry

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM