Java 类型已知,但方法指的是缺失的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36915966/
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-11 18:49:20  来源:igfitidea点击:

Type is known, but method refers to missing type

java

提问by Gui

I'm not very experienced with java and Eclipse and I'm getting the following problem:

我对 java 和 Eclipse 不是很有经验,我遇到了以下问题:

I'm writing something like:

我正在写这样的东西:

Point3D myPoint = myClass.myMethod(arg);

And I got the error:

我得到了错误:

the method myMethod(myType arg) refers to the missing type Point3D.

方法 myMethod(myType arg) 指的是缺少的类型 Point3D。

However the class Point3Dis known, I can create an object of this type (Point3D) without error and I got Point3Dmethods from auto-completion.

但是,该类Point3D是已知的,我可以毫无错误地创建此类型 ( Point3D)的对象,并且我Point3D从自动完成中获得了方法。

采纳答案by Redtama

You are evidently using a different implementation of Point3Din the class where you have declared the method than where you are calling it.

显然Point3D,您在声明方法的类中使用了与调用方法不同的实现。

Go to the declaration of myMethodand check that the import statement for Point3Din that class is the same as the import statement in the class where you are calling myMethod.

转到声明myMethod并检查Point3D该类中的 import 语句是否与您正在调用的类中的 import 语句相同myMethod

回答by Дмитрий Мурашов

I had the same problem. It was solved with casting returning value of method to needed class - in your case Point3D

我有同样的问题。它通过将方法的返回值转换为所需的类来解决 - 在您的情况下为 Point3D

    private static ConcurrentHashMap<Long,HotelInfHQ> HASH_HOTELINF = new  ConcurrentHashMap<Long,HotelInfHQ>();

////

public static HotelInfHQ getHotelInfByKey (Long key){   
    return (HotelInfHQ)HASH_HOTELINF.get(key);
}

and in another class call was like

在另一堂课上,电话就像

getHotelInfByKey(value);

Although ConcurrentHashMap HASH_HOTELINF was parametrized and Eclipse didn't show any mistake in return string, it was not obvious to it what class of object it returns at the point of method call

虽然 ConcurrentHashMap HASH_HOTELINF 参数化了,Eclipse 没有显示返回字符串有任何错误,但它在方法调用点返回什么类的对象并不明显

回答by Nick C

Might depend on your context, but I was having the same issue too until I compiled it. Make sure that Eclipse didn't hide your import statements at the top of your program by minimizing a couple lines. I accidentally imported a file from a previous project of the same name and it hid the import statement at the top: "import A3.BST;". Just expand out to see your lines and delete the import statement and you should be fine.

可能取决于您的上下文,但在编译之前我也遇到了同样的问题。通过最小化几行来确保 Eclipse 没有在程序顶部隐藏您的导入语句。我不小心从以前的同名项目中导入了一个文件,它在顶部隐藏了导入语句:“import A3.BST;”。只需展开以查看您的行并删除导入语句,您应该没问题。

回答by SpaceNet

I got same issue, I solved it by changing the order of import. I put the class which make the issue at the top of import list, after package, then the IDE(VS code) makes no alert.

我遇到了同样的问题,我通过更改导入顺序来解决它。我将导致问题的类放在导入列表的顶部,在打包之后,IDE(VS 代码)不会发出警报。