抽象類中的getClass()給出了Ambiguous方法調用

[英]getClass() in abstract class gives Ambiguous method call


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?

為什么會顯示這些錯誤,我怎么辦才能看不到它們?

5 个解决方案

#1


93  

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解決了錯誤。當然,您必須在項目中引用它才能使用修復程序。

#2


43  

The code is fine, but it is an error in IntelliJ.

代碼很好,但它在IntelliJ中是一個錯誤。

Error report, another one.

錯誤報告,另一個。

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。

#3


5  

Another working workaround is to generate a helper method to get the Class:

另一個工作方法是生成一個幫助方法來獲取類:

public Class<?> type() {
    return super.getClass();
}

or a static reusable util:

或靜態可重用的util:

public static final Class<?> type(Object object) {
    return object.getClass();
}

#4


3  

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.4 in the pom.xml linking to Maven Central.

我注意到它取決於您引用的Android版本是否發生錯誤。我曾經在鏈接到Maven Central的pom.xml中定義Android 4.1.1.4的依賴項。

<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()產生錯誤。

#5


1  

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平台作為項目SDK導入IntelliJ,則會發生這種情況。問題是Maven Android平台和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).

解決方法是goint to Project structure - > Platform Settings - > SDK - > Maven Android Platform - > Classpath。它將列出實際上在JDK中的所有jar。刪除所有這些,因此只剩下兩個Android依賴項(res和annotations.jar)。

EDIT: This problem has been already reported to IntelliJ issue tracker long time ago.

編輯:很久以前已經向IntelliJ問題跟蹤器報告了這個問題。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/04/30/b24bdf2ff5040d8d7d5f3e76af708c97.html



 
  © 2014-2022 ITdaan.com 联系我们: