在库开发中如何正确使用Java访问修饰符
我正在开发一个库,其他程序员将导入该库并将其用于他们的目的。
我对 Java 访问修饰符的目标感到困惑。
问题是我
- 在包
org.mylibrary
中的ClassA
- 下面有类
ClassB
在包org.mylibrary.internal
中
ClassA 需要解析 ClassB,因此 ClassB 需要是公共类。
但是,从图书馆用户的角度来看,我不希望 ClassB 在我的图书馆之外可见。因为它不应该也不需要由用户启动。
我考虑将 ClassB 移动到包 org.mylibrary 并使其成为包私有类。
如果我将它移到同一个包中,就会变得一团糟并且难以组织,因为在这种情况下我有很多类,所以在一个大包中会有很多 .java 文件。
通常我将类放在按类别或层分组的包中,我认为它很容易组织。
我该怎么做?人们如何处理这个问题?
I'm developing a library which the other programmer will import and use it for their purposes.
I'm confused about the objective of Java access modifier.
The problem is that I have classes below
ClassA
in packageorg.mylibrary
ClassB
in packageorg.mylibrary.internal
ClassA needs to resolve ClassB so ClassB need to be public class.
However, from library user view, I don't intend ClassB to be visible outside my library. Because it shouldn't be and don't need to be initiated by the user.
I think of moving ClassB to package org.mylibrary and make it package-private class.
If I move it to the same package, it would be a mess and difficult to organize because I have many classes in this scenario so there will be many .java files in a big one package.
Normally I put the classes in packages grouped by category or layer and I think it's easy to organize.
How do I do this? How do people handle this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很难给出具体的建议,因为您提供的关于 ClassA 和 ClassB 的角色和关系的信息太少。然而,一种通用的解决方案(几乎总是用作消除依赖关系的一部分)是将
ClassB
隐藏在接口后面。然后,ClassA
仅使用该接口,因此它不再直接依赖于ClassB
。ClassB
可以设为包私有,其实例由 工厂,或依赖注入到ClassA
中。It is difficult to give concrete advice since you give so little info about the roles of and relationship between
ClassA
andClassB
. However, one general solution (which is almost always used as part of eliminating dependencies) is to hideClassB
behind an interface. ThenClassA
uses only that interface, so it is not anymore directly dependent onClassB
.ClassB
can be made package private, its instances produced by e.g. a factory, or dependency injected intoClassA
.假设ClassB是一个带有单元测试的测试包:
为什么ClassB需要使用它。通常测试类使用常规类,反之亦然。
一般来说,测试类的建议是将它们放入与常规类相同的包中,但将 .java 文件维护在并行目录层次结构中(即,您有 src/org/mycompany/MyClass .java 和 test-src/org/mycompany/MyClassTest.java )。这样,对于 Java 来说,两者都在同一个包中并且可以相互访问,而对于发布版本,您只需不编译测试类(或者甚至不检查它们) - 这样一切都很好地分开。
如果这不适用于您的情况,也许您可以更详细地编辑您的问题?
Assuming ClassB is a test package with unit tests:
Why does ClassB need to use it. Normally test classes use the regular classes, not vice versa.
In general, the recommendation for test classes is to put them into the same package as the regular classes, but maintain the .java files in a parallel directory hierarchy (i.e. you have src/org/mycompany/MyClass.java , and test-src/org/mycompany/MyClassTest.java ). That way, to Java both are in the same package and can access each other, and for release builds you just don't compile the test classes (or don't even check them out) - that way everything is nicely separate.
If this does not apply in your case, maybe you could edit your question with more detail?
你的意思是java文件看起来很乱吗?您可以将内部类拆分到不同的 .java 文件中。
ClassA.java
Internal.java
希望这有帮助。
Do you mean that the java file look messy? You can split the internal classes in a different .java file.
ClassA.java
Internal.java
Hope this helps.
我想我理解你的问题,答案是到目前为止在java中还没有办法做到这一点!
有一些棘手的方法,但它们涉及污垢编码。
看这里
http://openide.netbeans.org/tutorial/api -design.html#design.less.friend
i think i understand your question, and the answer is that there is no way to do that in java up to now!
there are some tricky ways but they invovle dirt coding.
look here
http://openide.netbeans.org/tutorial/api-design.html#design.less.friend