[英]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
. 我一直在思考这个问题,为什么不能像在
.Net
的namespace
中那样定义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. 我在该程序包中的
Stack
和Queue
名称中有两个类。 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. 但是在这种情况下,我应该在
Stack
和Queue
类中对其进行定义,并且它也是多余的。 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中无法做到这一点!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.