Java嵌套包可见性 [英] Java nested package visibility

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

问题描述

通常在设计项目的某些新组件时,我会受到限制,无法以正确的方式进行锻炼...

Often when i'm designing some new component of the project I'm haveing a limitation that i cant right way to work out...

想象一个组件"包,在这个组件包中,您有一些公共类/接口可以自然地公开共享,还有一些内部包中需要的类/接口(ComponentSpecificStuff),但这些类/接口从外部是不可见的包装...

Imagine a package "component" and in this component package you have some public classes/interfaces which are naturally designed to share publically and some classes/interfaces (ComponentSpecificStuff) that are needed in inner packages but those should not be visible from outside of package...

现在,使用当前的Java可能性,我实现此目标的唯一方法是违反(不在组件"程序包之外公开Stuff")……

Now with current java possiblities only way I achieve this is by violating the (not expose Stuff outside "component" package) moment...

您如何解决此问题?

推荐答案

是的,您可以通过在包名称中使用内部关键字来做到一定程度. 例如,您有一个包裹

Yes you can do it till some extent by using internal keyword in package name.
Example you have a package

com.myPackage.component

您可以将另一个软件包作为

you can have another package as

com.myPackage.component.internal.blahblah

当您使用关键字internal时,它不会暴露在其他jar上,并且无法在jar之外使用,但可以由另一个包在jar内使用.在此,内部包将com.myPackage.component进一步扩展到层次结构中.正如 Jon Skeet的话 Packages are not hierarchical as far as the compiler is concerned - the "hierarchy" is just human pattern matching for organization, effectively.这就是我进一步扩展软件包的意思.

When you use the keyword internal it is not exposed to other jars and not available for usage outside the jar but can be used inside the jar by another package. Here the internal package is extending com.myPackage.component further down the hierarchy. As quote by Jon Skeet Packages are not hierarchical as far as the compiler is concerned - the "hierarchy" is just human pattern matching for organization, effectively. That is what I mean by extending the package further.

您可以通过不给类任何访问修饰符来定义私有的类包 示例:

You can define a class package private by not giving any access modifier to the class Example :

class PackagePrivateClass{

}

您可以在同一包中导入此类,但在该包外部不允许使用该类.

You can import this class in the same package but outside the package it doesn't allow you to use it.

这篇关于Java嵌套包可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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