java中的继承没有检测来自其他包的同名扩展类
我正在使用 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.Base
和Base
上有一条红线。它告诉我们要创建 Base
类,但它已经存在了。我尝试保存文件。我也尝试制作新项目。请帮忙。谢谢。
这是参考图片:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用 Maven 作为 Eclipse 项目的构建系统。
Maven 不允许主树中的类依赖于测试树中的类。它通过告诉 Java 编译器和运行时测试树中的类和资源不在主代码的构建时和运行时类路径中来防止这种情况发生。这就是您收到编译错误的原因。
解决方案:
将
Base.java
类移动到“src/main/java/Academy”目录中。您评论道:
抱歉...但是你错了。将类放入哪棵树确实很重要。这是一个 Maven 项目,Maven 对这些事情有“自己的看法”。有充分的理由,国际海事组织。但无论哪种方式,Maven 的意见优先......如果您选择使用 Maven。
还有一些其他问题:
包名称应以小写字母开头。例如,请阅读此。
解决方案:将
Academy
重命名为academy
1。非代码资源不应位于“java”目录中。 “java”源目录中的非代码资源不会位于运行时类路径中...并且不会包含在(常规)JAR 文件中。
解决方案:创建一个“resources”目录并将其放在那里。
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:
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:
A package name should start with a lowercase letter. Read this for example.
Solution: Rename
Academy
toacademy
1.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.
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.
导入
是无关紧要的。您的 src/main/java/、src/main/resources/ 是为您的主要产品打包的,可能类似于 E2EProject1.jar。
您的 src/test/java/、src/test/resources/ 打包在单元测试中,可能类似于 E2EProject1-test.jar。这些测试类保留在主产品之外,但将主产品作为依赖项放在类路径上。
所以它应该是这样的:
包的命名相同是为了能够测试(隐式导入) 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:
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.
按照以下步骤解决了上述错误:
右键单击项目 -> 构建路径 -> 配置构建路径 -> 源选项卡,
然后在项目 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