在库开发中如何正确使用Java访问修饰符

发布于 2024-09-24 03:48:31 字数 521 浏览 3 评论 0原文

我正在开发一个库,其他程序员将导入该库并将其用于他们的目的。

我对 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 package org.mylibrary
  • ClassB in package org.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绝影如岚 2024-10-01 03:48:31

很难给出具体的建议,因为您提供的关于 ClassA 和 ClassB 的角色和关系的信息太少。然而,一种通用的解决方案(几乎总是用作消除依赖关系的一部分)是ClassB隐藏在接口后面。然后,ClassA 仅使用该接口,因此它不再直接依赖于ClassBClassB 可以设为包私有,其实例由 工厂,或依赖注入ClassA中。

It is difficult to give concrete advice since you give so little info about the roles of and relationship between ClassA and ClassB. However, one general solution (which is almost always used as part of eliminating dependencies) is to hide ClassB behind an interface. Then ClassA uses only that interface, so it is not anymore directly dependent on ClassB. ClassB can be made package private, its instances produced by e.g. a factory, or dependency injected into ClassA.

白馒头 2024-10-01 03:48:31

假设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?

迷乱花海 2024-10-01 03:48:31

这会很混乱并且很难
组织起来,因为我有很多课
在这种情况下。

你的意思是java文件看起来很乱吗?您可以将内部类拆分到不同的 .java 文件中。

ClassA.java

package org.application;

public class ClassA {
}

Internal.java

package org.application;

class ClassB {
}

class SomeOtherInternalClass {
}

希望这有帮助。

It would be a mess and difficult to
organize because I have many classes
in this scenario.

Do you mean that the java file look messy? You can split the internal classes in a different .java file.

ClassA.java

package org.application;

public class ClassA {
}

Internal.java

package org.application;

class ClassB {
}

class SomeOtherInternalClass {
}

Hope this helps.

给不了的爱 2024-10-01 03:48:31

我想我理解你的问题,答案是到目前为止在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文