Java 如何保护类,使其在包外不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2534733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to protect classes so they are not visible outside their package
提问by Cam
I'd like to be able to have two "protected" classes in my package. That is, I do not want files outside of my package to see them as visible - they will be for internal use within the package only.
我希望能够在我的包中有两个“受保护”的类。也就是说,我不希望我的包外的文件将它们视为可见 - 它们仅供包内的内部使用。
How can I do this?
我怎样才能做到这一点?
采纳答案by Jason S
Just leave out all keywords. The default visibility is package-private, viewable within the package only.
只需省略所有关键字。默认可见性是package-private,只能在包内查看。
e.g.:
例如:
// class Foo is public
public class Foo
{
final private Bar bar = ...;
}
// class Bar is package-private
// (visible to all classes in the package, not visible outside the package)
class Bar
{
...;
}