Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌套的默认接口与嵌套的受保护接口有何不同?_Java_Interface - Fatal编程技术网

Java 嵌套的默认接口与嵌套的受保护接口有何不同?

Java 嵌套的默认接口与嵌套的受保护接口有何不同?,java,interface,Java,Interface,Java中嵌套的默认接口和嵌套的受保护接口之间有什么区别?为什么甚至允许嵌套的受保护接口 Test.java public class Test { // not implementable outside of current package interface NestedDefaultInterface { } // not implementable outside of current package? protected interface

Java中嵌套的默认接口和嵌套的受保护接口之间有什么区别?为什么甚至允许嵌套的受保护接口

Test.java

public class Test {
    // not implementable outside of current package
    interface NestedDefaultInterface {

    }

    // not implementable outside of current package?
    protected interface NestedProtectedIface {

    }
}

// both interfaces can be implemented
class Best implements Test.NestedProtectedIface, Test.NestedDefaultInterface { 

}
MyClass.java

class AClass implements Test.NestedProtectedIface { //Error

}

class AnotherClass implements Test.NestedDefaultInterface { //Error

}

class OneMoreClass extends Test implements Test.NestedProtectedIface { //Error

}

要显示视觉差异,请执行以下操作:

package com.one

public class Test {
    // not implementable outside of current package
    interface NestedDefaultInterface {

    }

    // implementable in child classes outside of package
    protected interface NestedProtectedIface {

    }
}
包装外:

package com.two

class SubTest extends Test { 
    public void testProtected() {
        NestedProtectedIface npi = new NestedProtectedIface () {
           // implementation
        };
    }

    public void testDefault() {
        // Won't compile!
    //    NestedDefaultInterface ndi = new NestedDefaultInterface() {
    //    };
    }
}
这里的混乱是关于可见性的

扩展类时,您将从
引用访问所有
受保护的
父属性

对于
default
access修饰符,它不会在包外工作

嵌套接口最流行的真实示例是
java.util.Map
Map.Entry


Map
的每个实现都提供自己的
条目
实现。(
HashMap
中的
Node
TreeMap中的
Entry
等)

添加了一个示例,您可以在其中找到嵌套接口的用法