在访问Java包,私有字段 [英] Accessing package-private fields in Java

查看:204
本文介绍了在访问Java包,私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对Android API源戳。有的FileDescriptor 与数据成员描述有没有访问修饰符:

Poking around Android API sources. There's FileDescriptor with a data member descriptor with no access modifier:

int descriptor;

此外,还有类的FileOutputStream 是构建一个新的FileDescriptor并分配到该字段:

Then there's class FileOutputStream that constructs a new FileDescriptor and assigns to that field:

fd = new FileDescriptor();
fd.descriptor = fileSystem.open(...);

这是怎么回事与Java的字段访问控制模型相兼容?我想包私有字段不能从声明类外部访问,并在C ++中有没有友谊的概念等。

How is that compatible with the field access control model of Java? I thought package-private fields are not accessible from outside the declaring class, and there's no notion of friendship like in C++.

推荐答案

基本上,包装和私营部门可以在访问类层次:

Basically, package-private can be accessed at the class and package levels:

源:

Access Levels
Modifier    Class   Package  Subclass World
public         Y        Y       Y       Y
protected      Y        Y       Y       N
no modifier    Y        Y       N       N
private        Y        N       N       N

这篇关于在访问Java包,私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆