简体   繁体   English

Java Eclipse:类无法解析为类型继承

[英]Java Eclipse: class cannot be resolved to a type inheritance

I've run into an issue where when I open projects in Eclipse there is something wrong with the visibility of classes across packages. 我遇到了一个问题,当我在Eclipse中打开项目时,包中的类的可见性出现了问题。

Example: 例:

package.a
   - public abstract class clazz
package.b
   - public abstract class clazzy extends clazz

In the above instance clazzy would have an error, where clazz wouldn't available. 在上面的例子中,clazzy会出现错误,clazz将无法使用。 If I move clazzy into package.a, and the move it back to package.b everything is fine, but otherwise I can't get eclipse to recognize that clazz exists at all. 如果我将clazzy移动到package.a中,并将其移回package.b一切都很好,但除此之外,我无法让eclipse认识到clazz存在。

EDIT 1: 编辑1:

Yes, I am importing the class. 是的,我正在导入课程。 I also press ctrl-shift-o (import). 我也按ctrl-shift-o(导入)。 If I hand type out the import, eclipse still fails to recognize that clazz exists. 如果我输入导入,eclipse仍然无法识别clazz存在。 I have not tried importing the entire package, as generally I avoid doing this for a whole variety of reasons. 我没有尝试导入整个包,因为我通常会因各种原因避免这样做。

Edit 2: 编辑2:

When I first open the project (it can already exist in the workspace) with the following imports: 当我第一次使用以下导入打开项目(它已经存在于工作区中)时:

import package.a.Clazz;

public abstract clazzy extends Clazz {
...
}

Both the import, and the clazz identifier will be underlined in red. 导入和clazz标识符都将用红色下划线标出。

I have received several suggestions to do: 我收到了一些建议:

import package.a;

Which not only does not resolve the issue, but I also would not want to do for a variety pf reasons. 这不仅不能解决问题,而且我也不希望出于各种原因。

Edit 3: 编辑3:

I should clarify that when I say "move" in the 2nd paragraph, i mean that if I move package.b.clazzy into another package (say package.a, or package.c) eclipse seems to realize it's being stupid and sorts itself out. 我应该澄清一下,当我在第2段中说“移动”时,我的意思是如果我将package.b.clazzy移动到另一个包(比如package.a或package.c),eclipse似乎意识到它是愚蠢的并且自己排序出。 If I then move clazzy back into package.b everything is fine from that point on. 如果我然后将clazzy移回package.b从那时起一切都很好。

This code compiles and runs correctly from that point on. 此代码从该点开始编译并正确运行。

I solved that with adding this import: 我通过添加此导入解决了这个问题:

import javax.persistence.InheritanceType;

hope it may help someone :) 希望它可以帮助某人:)

Your problem might be a naming issue... package is a keyword in Java and should not be used for names of a package, variable, class etc. 您的问题可能是命名问题... package是Java中的关键字,不应该用于包,变量,类等的名称。

Rename your package to a . 将您的包重命名a For that you either recreate it or move the content in a new directory named a and change the package identifier at the top of the class to package a . 为此,您可以重新创建它或将内容移动到名为a的新目录中,并更改类顶部的包标识符以package a

import the package like this 像这样导入包

import a.clazz;

not as package is th reserved keyword in java 不是包是java中的保留关键字

import package.a.clazz;

add the import in your code to inherit the class 在代码中添加导入以继承该类

If you don't want to import package.a.clazz (why not?) then you need to refer to it via its full name, 如果您不想导入package.a.clazz (为什么不?),那么您需要通过其全名来引用它,

public abstract class clazzy extends package.a.clazz {

But I really don't know why you would want to do that instead of 但我真的不知道你为什么要这样做而不是

import package.a.clazz;

public abstract class clazzy extends clazz {

In fact, the reason why the "move" in Eclipse works is because Eclipse, when it moves clazzy to the new package, automatically adds the import statement for you. 事实上,Eclipse中“移动”的原因是因为Eclipse在移动clazzy到新包时会自动为您添加import语句。

Also, be careful to use the right case. 另外,请注意使用正确的情况。 In your question, you used all lower case, but in your edits, you started referring to Clazz not clazz . 在您的问题中,您使用了全部小写,但在编辑中,您开始引用Clazz而不是clazz Those are different, where only the first ( Clazz ) follows standard conventions. 那些是不同的,只有第一个( Clazz )遵循标准惯例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM