Java 类型 A 已定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19611067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:45:47  来源:igfitidea点击:

Type A is already defined error

javaeclipse

提问by Susu

I tried to search for the solution, but what I found I don't know how to apply in this situation. Please help me correct my code.

我试图寻找解决方案,但我发现我不知道如何在这种情况下应用。请帮我更正我的代码。

package Exercise;

public class Ex11_11 {
public static void main(String[] args) {
    A a = new A(3);
    }
}

class A extends B { // type A is already defined, A has a red underline
    public A (int t) {
    System.out.println("A's constructor is invoked");
    }
}

class B { // type B is already defined, B has a red underline
    public B () {
        System.out.println("B's constructor is invoked");
    }  
}

采纳答案by Craigy

Well, the first thing to check is obviously whether or not you have another class called Ain your file or in the same package.

嗯,首先要检查的显然是A你的文件或同一个包中是否有另一个类被调用。

回答by Robin Green

Eclipse sometimes gets confused. If you choose Cleanfrom the Projectmenu, it might fix these errors.

Eclipse 有时会感到困惑。如果您CleanProject菜单中选择,它可能会修复这些错误。

回答by Валентин Стайков

Check if all your class files are saved. I've had this problem a few times: i define a class in a class file then move it in it's own one. Java gets confised, because it reads from the old version of the original file. Once you save it with the missing class definition in it and define the class in the new file all should be ok.

检查您的所有类文件是否已保存。我遇到过几次这个问题:我在类文件中定义了一个类,然后将它移动到它自己的类中。Java 被 confised,因为它从原始文件的旧版本中读取。一旦你用缺少的类定义保存它并在新文件中定义类,一切都应该没问题。

回答by Krzysztof Suchcicki

In your project you might have test directory with the same package structure and the same class name (for example copied without changing class name to *Test).

在您的项目中,您可能拥有具有相同包结构和相同类名的 test 目录(例如,在不将类名更改为 *Test 的情况下进行复制)。

回答by ironarm

I had the same problem. My computer was restarted remotely by I.T, and Eclipse did not shut down gracefully. I noticed there was an extra java file in my project that I didn't add. Deleted it, and now the error is gone.

我有同样的问题。我的电脑被 IT 远程重启了,Eclipse 没有正常关闭。我注意到我的项目中有一个我没有添加的额外 java 文件。删除它,现在错误消失了。

回答by Rohit Jain

In Project-> Clean, select "Clean projects selected below", select my project(s) and check "Start a build immediately" with "Build only selected projects".

在“项目”->“清理”中,选择“清理下面选择的项目”,选择我的项目并选中“立即开始构建”和“仅构建选定的项目”。

Then problem will resolve.

那么问题就会解决。

回答by arjun kumar mehta

The main reason for this is that somewhere in same package you have already defined a class with name A. which causes type Ais already defined error.

这样做的主要原因是在同一个包中的某个地方你已经定义了一个名为 A. 的类,这会导致类型A已定义错误。

check if there is any subclass or inner class named A

检查是否有任何名为A 的子类或内部类

回答by Kumar S

If none of the above solutions worked for you then it possible that Build Path is messed up. When you add a src to the build path make sure the src is not in the exclusion list. Place *(wild card) in the inclusion list and nothing in the exclusion list.

如果上述解决方案都不适用于您,那么构建路径可能会搞砸。当您将 src 添加到构建路径时,请确保 src 不在排除列表中。将 *(通配符)放在包含列表中,而在排除列表中不放置任何内容。

回答by Philip Rego

Make sure

确保

Project | Build Automatically

项目 | 自动构建

is checked.

被检查。

回答by NixRam

Have you added another project to the build path?

您是否在构建路径中添加了另一个项目?

I had the same issue on my development environment (Eclipse).
My Maven application consumed other applications. On Eclipse, those projects were just added to the build path. One of them had the same package structure and class filename. Eclipse ends up thinking both the files which are physically different are in the same directory since the package structure and filename are same.
For example, let's say there are two files as below:

我在我的开发环境(Eclipse)上遇到了同样的问题。
我的 Maven 应用程序消耗了其他应用程序。在 Eclipse 上,这些项目只是添加到构建路径中。其中之一具有相同的包结构和类文件名。由于包结构和文件名相同,Eclipse 最终认为物理上不同的文件位于同一目录中。
例如,假设有两个文件如下:

/Users/uname/home/proj1/com/app/proj/main/java/util/file1.java

and

/Users/uname/home/proj2/com/app/proj/main/java/util/file1.java

and lets say both have the package name

让我们说两者都有包名

com.app.define.proj.util

If you add one of the project to the other, Eclipse would consider both the files to be in the same location.
I resolved by creating a JAR file out of the consumed application, adding it to the build path and removing the Eclipse project from build path.

如果将一个项目添加到另一个项目,Eclipse 会认为这两个文件位于同一位置。
我通过从使用的应用程序中创建一个 JAR 文件,将其添加到构建路径并从构建路径中删除 Eclipse 项目来解决。