Java JDK11基于嵌套的访问控制的实现
作者:Spring中文网 时间:2021-07-11 10:02:05
Java(和其他语言)通过内部类支持嵌套类。要使其正常工作,需要编译器执行一些技巧。这是一个例子:
public class Outer {
private int outerInt;
class Inner {
public void printOuterInt() {
System.out.println("Outer int = " + outerInt);
}
}
}
在执行编译之前,编译器会修改它以创建类似的东西:
public class Outer {
private int outerInt;
public int access$000() {
return outerInt;
}
}
class Inner$Outer {
Outer outer;
public void printOuterInt() {
System.out.println("Outer int = " + outer.access$000());
}
}
虽然从逻辑上讲,内部类是与外部类相同的代码实体的一部分,但它被编译为一个单独的类。因此,它需要编译器创建合成桥接方法,以提供对外部类的私有字段的访问。
这个JEP引入了巢的概念,其中同一巢的两个成员(我们的例子中的外部和内部)是同窝。为类文件格式NestHost和NestMembers定义了两个新属性。这些更改对于支持嵌套类并编译为字节码的其他语言非常有用。
此功能为java.lang.Class引入了三个新方法:
Class getNestHost()
Class[] getNestMembers()
boolean isNestmateOf(Class)
此功能还需要更改Java虚拟机规范(JVMS),特别是第5.4.4节“访问控制”。
来源:https://juejin.im/post/5c47f687e51d4503093b5711
标签:Java,JDK11,嵌套,访问
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java SpringBoot启动指定profile的8种方式详解
2023-07-31 21:48:03
![](https://img.aspxhome.com/file/2023/1/57831_0s.jpg)
Flutter图片与文件选择器使用实例
2023-07-17 09:11:12
![](https://img.aspxhome.com/file/2023/0/106030_0s.png)
详解SpringBoot Start组件开发之记录接口日志信息
2023-07-26 18:33:34
![](https://img.aspxhome.com/file/2023/5/57835_0s.jpg)
java实现Base64加密解密算法
2023-11-25 08:07:27
flutter Bloc 实现原理示例解析
2023-07-18 08:00:39
深入解析Spring Cloud内置的Zuul过滤器
2022-05-16 12:49:04
Java实现单链表反转的多种方法总结
2023-11-11 02:28:08
![](https://img.aspxhome.com/file/2023/1/58721_0s.png)
SpringBoot构建ORM框架的方法步骤
2023-10-31 20:41:21
![](https://img.aspxhome.com/file/2023/3/58543_0s.png)
超全MyBatis动态代理详解(绝对干货)
2023-11-14 02:28:19
![](https://img.aspxhome.com/file/2023/2/59082_0s.jpg)
SpringBoot详细讲解yaml配置文件的用法
2022-04-21 16:29:33
![](https://img.aspxhome.com/file/2023/0/61030_0s.png)
Java常见数据结构面试题(带答案)
2023-11-24 19:44:05
Java调用CXF WebService接口的两种方式实例
2023-11-09 02:25:11
![](https://img.aspxhome.com/file/2023/7/58987_0s.jpg)
SpringBoot打成war包在tomcat或wildfly下运行的方法
2023-11-23 08:20:56
关于Mybatis-Plus Wrapper是否应该出现在Servcie类中
2023-11-28 22:04:56
SpringBoot2使用Jetty容器操作(替换默认Tomcat)
2023-11-24 01:17:15
![](https://img.aspxhome.com/file/2023/9/59779_0s.jpg)
SpringBoot如何获取Kafka的Topic列表
2023-11-26 16:01:52
![](https://img.aspxhome.com/file/2023/2/60242_0s.png)
c#中CAD文件读取实例
2023-07-23 19:37:59
JSON复杂数据处理之Json树形结构数据转Java对象并存储到数据库的实现
2023-09-17 17:03:59
IDEA集成MyBatis Generator插件的使用
2023-08-12 00:28:47
![](https://img.aspxhome.com/file/2023/6/57896_0s.png)
Java语言实现Blowfish加密算法完整代码分享
2023-11-02 21:30:07