简体   繁体   English

有什么方法可以在Java中定义一个私有类,只有同一包中的其他类才能访问它?

[英]Is there any way to define a private class in Java which only other classes in the same package can access it?

I've always had this question on top of my mind that why defining private classes within a package in Java is not possible like the way we can define it in a namespace in .Net . 我一直在思考这个问题,为什么不能像在.Netnamespace中那样定义Java包中的private类,这是不可能的。

Let's say I have a ds package in my Java Project. 假设我的Java项目中有一个ds包。 I have two classes in the names of Stack and Queue inside this package. 我在该程序包中的StackQueue名称中有两个类。 Both of these classes need to make use of another class in the name of ListNode . 这两个类都需要使用ListNode名称中的另一个类。 ListNode is only required by other classes in the ds package, and it shouldn't be exposed to classes outside of the ds package. 只有ds包中的其他类才需要ListNode ,并且不应将其暴露给ds包之外的类。

I can define the ListNode as an inner classes. 我可以将ListNode定义为内部类。 But in this case, I should define it in both Stack and Queue classes, and it's also redundant. 但是在这种情况下,我应该在StackQueue类中对其进行定义,并且它也是多余的。 Is there any design pattern to work around this? 有没有解决此问题的设计模式? If not, please someone at least explain me why this is not possible in Java! 如果没有,请至少有人向我解释为什么在Java中无法做到这一点!

You can look into package private classes. 您可以查看包私有类。 docs docs

If you omit the public/private/protected from your class it is only accessible by other classes in the same package 如果您在课程中省略了公共/私人/受保护的课程,则只能由同一软件包中的其他课程访问

The following table shows the access to members permitted by each modifier. 下表显示了对每个修饰符允许的成员的访问。

Access Levels
Modifier    Class   Package Subclass    World
public       Y        Y        Y          Y
protected    Y        Y        Y          N
no modifier  Y        Y        N          N
private      Y        N        N          N        

From here 这里

暂无
暂无

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

相关问题 如何在Java中创建对同一包中的其他类不可见的(私有)类? - How to make a (private) class in java that is not visible to other classes in the same package? 在 Java 类中定义 `package private` 访问修饰符 - Define `package private` access modifier in Java class 如果两个类都在同一包中,则子类可以访问父类的私有成员吗? - Can a subclass access parent class private members if both the classes are in the same package 有什么方法可以模拟其他 class 中的私有方法 - Is there any way to Mock the private method , which is there in other class 有没有一种方法可以在Java中使用package-private来允许其他软件包访问? - Is there a way to use package-private in java to allow other packages access? 如何创建一个可执行的 jar 文件,该文件可以执行同一 package 中其他 java 类的方法 - how to create a executable jar file which can execute the methods from other java classes in same package Java类位于同一包(不同的目录)中,但它们不能彼此访问 - Java classes are in the same package (different directories) but they can't access each other 为什么外部 Java 类可以访问内部类私有成员? - Why can outer Java classes access inner class private members? 为什么在其他包中定义类无法在java中的默认包中实现接口或扩展类定义 - why class define in other package can not implement interface or extends class define in default package in java 如何在 JAVA 中调用同一包中的其他类的方法或访问字段 - How can I invoke a method or access a field from other class in the same package in JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM