java中interface之间嵌套_java – 嵌套到接口中的类

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

它有意义吗?它们不能存在于包含接口的实例中,因为接口无法实例化,因此……

以下编译:

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个班级之间有什么区别吗?实际上是静态的吗?

请注意,如果使用类更改接口,则显然会在InnerClass的static 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

A member type declaration in an interface is implicitly static and

public. It is permitted to redundantly specify either or both of these

modifiers.

static class InnerClass

因为它在一个界面中.

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

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

是的,StaticNestedClass和InnerClass是一样的

标签:java,static,interface,jls,nested-class

来源: https://codeday.me/bug/20190629/1324929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值