java中的继承没有检测来自其他包的同名扩展类

发布于 2025-01-15 20:57:31 字数 794 浏览 3 评论 0 原文

我正在使用 Eclipse。

2 个文件夹:src/test 和 src/main

都有一个同名的包:Academy

src/test 中的 Academy 包有一个类名:Base.java

Academy 包位于< strong>src/main 中有一个类名: HomePage.java

HomePage.java 类中,extends Base 是没有检测到其中的基类。 这是HomePage.java内的代码,


package Academy;

import Academy.Base;

public class HomePage extends Base{
}

Academy.BaseBase上有一条红线。它告诉我们要创建 Base 类,但它已经存在了。我尝试保存文件。我也尝试制作新项目。请帮忙。谢谢。

这是参考图片:

I am using Eclipse.

2 folders: src/test and src/main

Both have a package with same name: Academy.

The Academy package in src/test has in it a class name: Base.java

The Academy package in src/main has in it a class name: HomePage.java

In the HomePage.java class, extends Base is not detecting base class in it.
This is the code inside HomePage.java


package Academy;

import Academy.Base;

public class HomePage extends Base{
}

There is a red line on Academy.Base and on Base. It tells to make Base class, but it is already there. I tried to save the file. I tried to make new project too. Please Help. Thank you.

Here is the reference image:

refImage

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

·深蓝 2025-01-22 20:57:31

您正在使用 Maven 作为 Eclipse 项目的构建系统。

Maven 不允许主树中的类依赖于测试树中的类。它通过告诉 Java 编译器和运行时测试树中的类和资源不在主代码的构建时和运行时类路径中来防止这种情况发生。这就是您收到编译错误的原因。

解决方案:

Base.java类移动到“src/main/java/Academy”目录中。


您评论道:

这只是 2 个同名的包,位于 2 个不同的文件夹中。不管是不是test,都应该用extends继承。

抱歉...但是你错了。将类放入哪棵树确实很重要。这是一个 Maven 项目,Maven 对这些事情有“自己的看法”。有充分的理由,国际海事组织。但无论哪种方式,Maven 的意见优先......如果您选择使用 Maven。


还有一些其他问题:

  1. 包名称应以小写字母开头。例如,请阅读

    解决方案:将Academy重命名为academy1

  2. 非代码资源不应位于“java”目录中。 “java”源目录中的非代码资源不会位于运行时类路径中...并且不会包含在(常规)JAR 文件中。

    解决方案:创建一个“resources”目录并将其放在那里。

  3. import Academy.Base; 是多余的。没有必要在同一个包中导入类。


1 - 替代解决方案:不要要求其他人处理您的代码,也不要帮助您。如果其他人永远不需要阅读您的代码,那么您只关心代码的外观。编译器不在乎。只有人类才会这样做。

You are using Maven as the build system for your Eclipse project.

Maven does not allow a class in your main tree to depend on a class in the test tree. It prevents this by telling the Java compiler and runtime that classes and resources in the test tree are not in the build time and runtime classpaths for the main code. That is why you are getting the compilation error.

Solution:

Move the Base.java class into "src/main/java/Academy" directory.


You commented:

It's just 2 packages with same name, inside 2 different folders. whether it's test or not, it should still inherit with extends.

Sorry ... but you are wrong. It does matter which tree you put the classes in. This is a Maven project, and Maven is "opinionated" about these things. For good reason, IMO. But either way, Maven's opinion takes precedence ... if you choose to use Maven.


There are a couple of other problems:

  1. A package name should start with a lowercase letter. Read this for example.

    Solution: Rename Academy to academy1.

  2. Non-code resources should not be in a "java" directory. Non-code resources in a "java" source directory won't be on the runtime classpath ... and won't be included in (regular) JAR files.

    Solution: Create a "resources" directory and put it there.

  3. The import Academy.Base; is redundant. It is not necessary to import a class in the same package.


1 - Alternate solution: don't ask other people to work on your code, or help you with it. If other people don't ever need to read your code, it only matters to you what your code looks like. The compiler doesn't care. Only humans do.

离旧人 2025-01-22 20:57:31

导入是无关紧要的。

您的 src/main/java/、src/main/resources/ 是为您的主要产品打包的,可能类似于 E2EProject1.jar。

您的 src/test/java/、src/test/resources/ 打包在单元测试中,可能类似于 E2EProject1-test.jar。这些测试类保留在主产品之外,但将主产品作为依赖项放在类路径上。

所以它应该是这样的:

  • src/main/java/Base.java
  • src/test/resources/data.properties (或 src/main/resources/)

包的命名相同是为了能够测试(隐式导入) non-public,将src/main/java的私有类封装在src/test/java中的单元测试类中。

The import was irrelevant.

Your src/main/java/, src/main/resources/ are packed for your main product, probably something like E2EProject1.jar.

Your src/test/java/, src/test/resources/ are packed in your unit tests, probably something like E2EProject1-test.jar. These test classes are kept outside your main product, but have the main product on the class path as dependency.

So it should be something like:

  • src/main/java/Base.java
  • src/test/resources/data.properties (or src/main/resources/)

That the packages are named identical is for being able to test (implicitly import) non-public, package private classes of src/main/java in unit test classes in src/test/java.

独孤求败 2025-01-22 20:57:31

按照以下步骤解决了上述错误:

右键单击项目 -> 构建路径 -> 配置构建路径 -> 源选项卡,

然后在项目 src/main/java 下搜索选项卡搜索

并更改“包含测试源”从否到是
并应用更改

Solved the above error by following these steps:

Right click on project->Build path->Configure build path->source tab

then search tab search under your project src/main/java

and change "contains test sources" from No to Yes
and apply the changes

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