Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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_Static_Nested Class_Jls - Fatal编程技术网

Java 嵌套到接口中的类

Java 嵌套到接口中的类,java,interface,static,nested-class,jls,Java,Interface,Static,Nested Class,Jls,为什么可以将内部(也称为非静态嵌套)类定义到接口中? 这有什么意义吗?它们不能存在于包含接口的实例中,因为接口无法实例化,所以 以下内容不适用: interface MyInterface { static class StaticNestedClass { static int a() { return 0; } int b() { return 1;

为什么可以将内部(也称为非静态嵌套)类定义到接口中?

这有什么意义吗?它们不能存在于包含接口的实例中,因为接口无法实例化,所以

以下内容不适用:

interface MyInterface
{
    static class StaticNestedClass
    {
        static int a()
        {
            return 0;
        }
        int b()
        {
            return 1;
        }
    }
    class InnerClass
    {
        static int a()
        {
            return 0;
        }
        int b()
        {
            return 1;
        }
    }
}
以上两个班有什么不同吗?是否实际考虑了
静态
?
请注意,如果使用
class
更改
接口
,显然会在
内部类
静态int a()
上出现编译错误

此外,请查看以下内容:

interface MyInterface
{
    int c=0;
    static class StaticNestedClass
    {
        static int a()
        {
            return c;
        }
        int b()
        {
            return c+1;
        }
    }
    class InnerClass
    {
        static int a()
        {
            return c;
        }
        int b()
        {
            return c+1;
        }
    }
}
与外部包含实体是类不同,这里当然没有“内部(非静态嵌套)类可以访问外部的字段,而静态嵌套类不能访问外部的字段”这样的事情,因为,考虑到外部实体是一个接口,我们的
c
整数是隐式静态的。。。
接口的嵌套类也是隐式静态的吗

那么,
StaticNestedClass
InnerClass
是一样的吗

class InnerClass 
隐式(由编译器根据转换为)

接口中的成员类型声明是隐式静态的,并且 公众。允许冗余指定其中一项或两项 修饰语

因为它在一个接口中

将接口更改为类时会出现错误,因为不允许使用非静态内部类,并且在类的情况下不会隐式转换为静态

要直接回答你的最后一个问题


是的,
StaticNestedClass
InnerClass
是一样的

我认为
class InnerClass
是隐式静态的,类似于
c
@Cruncher事实上,编译器会自动将接口中的任何非方法标记为
static
。谈到接口成员是隐式的
静态的
,其中包括成员类。@我在寻找特定的JLS部分。我遇到了这一点,不确定这一部分是否涵盖了内部类。谢谢,将添加到帖子中。编辑:没关系,我在9.3,9.5是相当明确的这个感谢你克朗彻和吉特曼,JLS 9.5节正是我要找的。
static class InnerClass