简体   繁体   English

使包受保护的Java类在父包中可见

[英]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视为同一级别 - 只是同一列表中的元素。

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(对所有类可见)。

As far as i know it is not possible in Java. 据我所知,在Java中是不可能的。 For more information see the link here which explains the access modifiers in Java clearly. 有关更多信息,请参阅此处的链接,该链接清楚地解释了Java中的访问修饰符。

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

相关问题 为什么包保护的方法在同一个包中不可见? - Why is package-protected method not visible in the same package? 在受包保护的类的ctor上调用newInstance - invoking newInstance on package-protected class's ctor 如何访问我无法修改的类中的包保护变量 - How to access package-protected variable in class I cannot modify Java类在某些父包中吗? - Is Java class in certain parent package? Java中的包访问(受保护的修饰符) - Package Access (Protected Modifier) in Java 当父级位于不同的程序包中且子类为Abstract时,模拟受保护的父级方法 - Mock Protected Parent Method When the Parent is in a Different Package and the Child Class is Abstract 如何在Java中创建对同一包中的其他类不可见的(私有)类? - How to make a (private) class in java that is not visible to other classes in the same package? Java - 为什么另一个 package 中的子级无法通过父级引用访问父级的受保护方法? - Java - Why Child in another package cannot access parent's protected method through parent reference? 父 package class 可从 java 中的子包 class 访问? - parent package class accessible from child packge class in java? Java访问不同包中子类中的受保护成员,使用父类型的对象引用 - Java access to protected member in subclass in different package, using object reference of parent type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM