java 抽象类中的 getClass() 给出了模糊的方法调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10386264/
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
getClass() in abstract class gives Ambiguous method call
提问by Matsemann
I have a public abstract class and I'm trying to use the getClass()
method, as I will need info from the class extending my abstract class. An example is this:
我有一个公共抽象类,我正在尝试使用该getClass()
方法,因为我需要来自扩展我的抽象类的类的信息。一个例子是这样的:
public String getName() {
return getClass().getSimpleName();
}
However, IntelliJ reports this:
但是,IntelliJ 报告了这一点:
Ambiguous method call. Both
getClass () in Object and
getClass () in Object match.
The code runs fine, but having tens of error warnings in my IDE is kinda in my way. It disrupts my work flow with a lot of false positives.
代码运行良好,但在我的 IDE 中有数十个错误警告有点妨碍我。它以大量误报扰乱了我的工作流程。
Why are these errors shown, and what can I do to not see them?
为什么会显示这些错误,我该怎么做才能看不到它们?
采纳答案by Matsemann
The code is fine, but it is an error in IntelliJ.
代码没问题,但在 IntelliJ 中是错误的。
There are even some more error reports with different variations of this issue. As duffymo pointed out in comments, it can also be because there are different versions of the JDK in the classpath.
甚至还有更多错误报告包含此问题的不同变体。正如 duffymo 在评论中指出的那样,也可能是因为类路径中存在不同版本的 JDK。
回答by keyboardsurfer
Casting my getClass()
call to Object
like this
把我的getClass()
电话Object
打成这样
((Object) this).getClass()
solves the problem (with non abstract classes) for me. It's not great, but it's working.
为我解决了问题(使用非抽象类)。这不是很好,但它正在起作用。
Also, manipulating your Android SDKs from the project settings and removing all JDK jars from your Android SDK resolves the error. Of course you'll have to reference it within your project to utilize the fix.
此外,从项目设置中操作您的 Android SDK 并从您的 Android SDK 中删除所有 JDK jar 可以解决该错误。当然,您必须在项目中引用它才能使用此修复程序。
回答by pablisco
Another working workaround is to generate a helper method to get the Class:
另一种解决方法是生成一个辅助方法来获取类:
public Class<?> type() {
return super.getClass();
}
or a static reusable util:
或静态可重用工具:
public static final Class<?> type(Object object) {
return object.getClass();
}
回答by JJD
I noticed that it depends on the Android version you are referencing whether the error occurs or not. I used to define a dependency for Android 4.1.1.4in the pom.xml
linking to Maven Central.
我注意到无论是否发生错误,这取决于您所引用的 Android 版本。我曾经在链接到Maven Central 时为Android 4.1.1.4定义了一个依赖项。pom.xml
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
Meanwhile, I provide the needed dependencies using maven-android-sdk-deployer.
同时,我使用maven-android-sdk-deployer提供所需的依赖项。
<dependency>
<groupId>android</groupId>
<artifactId>android</artifactId>
<version>4.3_r1</version>
<scope>provided</scope>
</dependency>
Then getClass()
produces the error.
然后getClass()
产生错误。
回答by WonderCsabo
This can happen if you have an Android project with Maven, and you import into IntelliJ using the Maven Android Platform
as the project SDK. The problem is that both Maven Android Platform
and the Android maven dependency jar includes the java.lang.Object
class.
如果您有一个带有 Maven 的 Android 项目,并且您使用Maven Android Platform
作为项目 SDK 的项目导入 IntelliJ,则可能会发生这种情况。问题是Maven Android Platform
Android maven 依赖 jar 都包含java.lang.Object
该类。
The workaround is goint to Project structure-> Platform Settings-> SDK-> Maven Android Platform-> Classpath. It will list all the jars which are in JDK actually. Remove all of them, so only the two Android dependency remains (res
and annotations.jar
).
解决方法是Project structure-> Platform Settings-> SDK-> Maven Android Platform-> Classpath。它将列出实际在 JDK 中的所有 jar。删除所有这些,所以只剩下两个 Android 依赖项(res
和annotations.jar
)。
EDIT: This problem has been already reportedto IntelliJ issue tracker long time ago.
编辑:这个问题很久以前就已经报告给 IntelliJ 问题跟踪器。