[英]Making package-protected java class visible in parent package
I have two packages 我有两个包
uk.co.planetbeyond.data
and 和
uk.co.planetbeyond.data.bean
How can I make the classes present in uk.co.planetbeyond.data.bean
visible to classes in parent package uk.co.planetbeyond.data
but invisible in other packages? 如何才能使uk.co.planetbeyond.data.bean
中的类对父包uk.co.planetbeyond.data
类可见,但在其他包中不可见?
Is it even possible? 它甚至可能吗?
As far as I'm aware, it's not possible. 据我所知,这是不可能的。 While we tend to think of packages as forming a hierarchy, they don't as far as the Java language is concerned. 虽然我们倾向于认为包形成层次结构,但它们并不涉及Java语言。
As stated in all the other answers: No. The package hierarchy means nothing outside of the mind of the person who set it up. 正如所有其他答案中所述:不。包装层次结构意味着设置人员的思想之外没有任何东西。
In my opinion this is Java's major flaw. 在我看来,这是Java的主要缺陷。 When building up monster systems by merging together other monster programs, nothing beats Java-the-language. 通过将其他怪物程序合并在一起构建怪物系统时,没有什么能比Java语言更好。 All other languages eventually end up in confused, undocumentable, indecipherable piles of code. 所有其他语言最终都会陷入混乱,无法记录,难以理解的代码堆中。
Well, so does Java, but it gets a lot further than any other language I've used. 那么,Java也是如此,但它比我使用的任何其他语言都要进一步发展。 But its ignorance of package hierarchy--the inability to encapsulate data in a package hierarchy--is the limitation, or brick wall, that stops it. 但是它对包层次结构的无知 - 无法将数据封装在包层次结构中 - 是阻止它的限制或砖墙。 I can put a system, or set of classes that work together, in a single package. 我可以将一个系统或一组一起工作的类放在一个包中。 Then I can make a bunch of those classes package private so that when this package is added to another super-system that super-system cannot see--and be confused by--those hidden classes. 然后我可以将这些类包中的一堆私有化,这样当这个包被添加到另一个超级系统时,超级系统无法看到 - 并且被这些隐藏的类弄糊涂了。
But if my system is already super enough ("super" as in "superman", not "superclass") to need several packages for clarity, my formerly package-private classes must now be public, and I can't stop the super-super-system from seeing everything in my super-system. 但是,如果我的系统已经足够超级(“超级”,如“超人”,而非“超级”)需要几个包清楚,我以前的包私有类现在必须是公开的,我不能阻止超级 -超级系统从我的超级系统中看到一切。
To make a vast, complex system work, the complexity at every point must be minimized. 要使庞大而复杂的系统工作,必须尽量减少每个点的复杂性。 My super-system has to look as simple as possible to the super-super-system I'm adding it to. 我的超级系统必须看起来尽可能简单到我正在添加它的超级超级系统。 Having all the pipes and wires and beams and welds sitting out in public display because I cannot make them package-level-private does not help this. 所有的管道和电线以及横梁和焊缝都是公开展示的,因为我无法将它们打包成私有包装并没有帮助。
I hope Jigsaw, mentioned elsewhere, will help. 我希望其他地方提到的Jigsaw会有所帮助。 I also hope developer tools such as IDEs will start displaying packages in an outline format rather than treating ab and abcde as if they were all on the same level--just elements in the same list. 我也希望IDE之类的开发人员工具能够以大纲格式开始显示包,而不是将ab和abcde视为同一级别 - 只是同一列表中的元素。
Short answer, it's not possible unless for subclasses of classes in your uk.co.planetbeyond.data.bean package. 简短的回答,除非您的uk.co.planetbeyond.data.bean包中的类的子类,否则它是不可能的。
You may be interested in reading: 您可能有兴趣阅读:
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
Isn't "package private" member access synonymous with the default (no-modifier) access? “包私有”成员访问是否与默认(无修饰符)访问同义?
In Java, difference between default, public, protected, and private 在Java中,默认,公共,受保护和私有之间的区别
Java - Method accessibility inside package-private class? Java - 包私有类中的方法可访问性?
Currently, this is not possible. 目前,这是不可能的。 Java packages are not really hierarchical in nature - classes are either in the same package or they are not. Java包本质上不是分层的 - 类可以在同一个包中,也可以不在。
However, this is likely one of the things that will be addressed by Project Jigsaw , which may be included in Java 8. That should allow creating classes which are public within all the packages of a module, but are not exported to other modules. 但是,这可能是Project Jigsaw将要解决的问题之一,它可能包含在Java 8中。这应该允许创建在模块的所有包中公开的类,但不会导出到其他模块。
No, it is not possible. 不,这是不可能的。 Package visibility cannot selectively extend to other packages. 包可见性无法有选择地扩展到其他包。 A class may only be private (that is, visible only to a non-private enclosing class), package-private (no modifier, visible to only to other classes in the same package), or public (visible to all classes). 类只能是私有的(即,仅对非私有封闭类可见),package-private(无修饰符,仅对同一包中的其他类可见)或public(对所有类可见)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.