简体   繁体   English

Java中的包访问(受保护的修饰符)

[英]Package Access (Protected Modifier) in Java

Suppose I have a package: 假设我有一个包:

package com.g00gle.car

and

package com.g00gle.car.stereo

Is it possible to have a class in com.g00gle.car to access a class member in com.google.car.stereo? 是否可以在com.g00gle.car中创建一个类来访问com.google.car.stereo中的类成员? (Assuming that class member is labeled protected). (假设类成员被标记为受保护)。

The answer is no... (by default) but is there a way to circumvent that? 答案是否定的......(默认情况下),但有没有办法规避这一点? I have an application that I want to cut into distinct chunks... and to do that, I create extensions of the package. 我有一个应用程序,我想切成不同的块...并为此,我创建包的扩展。 What's discouraging is the loss of package private access. 令人沮丧的是丢失了包私有访问。

This need will be addressed in Java 7 with superpackages . 这种需求将在具有超级包的 Java 7中得到解决。 Or at least it was going to be. 或者至少它会是。 Everything about Java 7 is currently up in the air. Java 7的所有内容目前都在播出。

Edit 编辑

Thanks for the link from Peter Štibraný in the comments below. 感谢PeterŠtibraný在下面的评论中提供的链接。 Mark Reinhold's blog indicates this has been moved to Java 8 for release sometime in 2012 (take both the release and the release date with a grain of salt). 马克·莱因霍尔德(Mark Reinhold)的博客表明,这一问题已经在2012年的某个时候发布到Java 8上(同时发布和发布日期还有一点点)。

You can throw in a method in com.google.car.[classname] that provides access to the protected member: 您可以在com.google.car。[classname]中引入一个方法,该方法提供对受保护成员的访问:

protected Member member;

...

public Member getMember(){
   return member;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM