为什么可以将内部(又名非静态嵌套)类定义到接口中?
这有什么意义吗?它们不能存在于包含接口的实例中,因为接口不能被实例化,所以......
以下确实编译:
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;
}
}
}
以上2个类之间有什么区别吗?实际是static考虑到了吗?请注意,如果您更改interface为' class,显然会出现编译错误。InnerClassstatic 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整数是隐式静态的...interface的嵌套类也是隐式静态的吗?
所以再一次,是StaticNestedClass和InnerClass只是一样吗?