English 中文(简体)
抽象的分类 静态方法电话
原标题:getClass() in abstract class gives Ambiguous method call

我有一个公开的抽象阶级和Im试图使用getClass()方法,因为我需要从延长我的抽象阶级的那类人中提取信息。 例如:

public String getName() {
    return getClass().getSimpleName();
}

However, IntelliJ reports this:

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.

Why are these errors shown, and what can I do to not see them?

最佳回答

该法典是罚款,但这是英特尔利J的错误。

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.

问题回答

Casting my getClass() call to Object like this

((Object) this).getClass()

solves the problem (with non abstract classes) for me. It s not great, but it s working.

此外,从项目环境中操纵你的安康基斯,从您的安康K公司中删除所有JDK jars,也解决了错误。 当然,你不得不在你的项目内提及它,以利用固定装置。

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

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

或静态可再利用:

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

我注意到,这取决于你指的是错误是否发生。 我用于界定对Android 4.1.1.4/a>。

<dependency>
    <groupId>com.google.android</groupId>
    <artifactId>android</artifactId>
    <version>4.1.1.4</version>
    <scope>provided</scope>
</dependency>

同时,我通过maven-android-sdk-deployer提供所需的附属条件。

<dependency>
    <groupId>android</groupId>
    <artifactId>android</artifactId>
    <version>4.3_r1</version>
    <scope>provided</scope>
</dependency>

然后getClass()产生错误。

如果你有一个与Maven一起的安康项目,并且你将Maven 平台作为项目SDK输入英特尔利J,就可以做到这一点。 问题在于Maven 安信平台和Andan maven Depend jar均包括java.lang。 目标

工作范围为项目结构->Platform ->SDK ->Mavenony平台->Classpath。 这份清单将列出实际存在的JDK的所有支柱。 移除所有保留,因此只有两种依附保留(resannotations.jar)。

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...