Java名称冲突错误,一种方法与另一种方法具有相同的擦除 [英] Java name clash error, a method has the same erasure as another method

查看:2124
本文介绍了Java名称冲突错误,一种方法与另一种方法具有相同的擦除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,如下所示:

类QueryResult:

pre $ code > public class QueryResult
{
...
public static List sortResults(boolean ascending,List< QueryResult> toSort)
{...
}
}

和类CaseResult:

<$ p $ b $ public class CaseResult extends QueryResult
{
...
public static List sortResults(boolean ascending,List< CaseResult> toSort)
{ ..
}
}

我收到以下错误:
$ b


名称冲突:类型
的sortResults(boolean,List)方法CaseResult与sortResults具有相同的删除操作(布尔型,
列表)类型QueryResult,但不隐藏
它CaseResult.java


我在相似的问题 Java泛型名称冲突,具有相同的擦除次数,但出现更多错误。我想我可能会误解某些东西,或者这些答案不适合我的情况。

有人可以提供任何解决方案或解释以帮助我理解吗?谢谢大家。

解决方案

根据 Java Documentation



如果一个子类定义了一个静态方法,静态方法,然后子类中的方法隐藏超类中的方法。



隐藏静态方法和重写实例方法之间的区别具有重要意义:
$ b


  • 被调用的重写实例方法的版本是子类中的版本。

  • 被调用的隐藏静态方法的版本取决于它是从超类还是子类调用


I have two classes as follows

class QueryResult:

public class QueryResult
{
...
public static List sortResults(boolean ascending, List<QueryResult> toSort)
    {   ...
    }
}

and class CaseResult:

public class CaseResult extends QueryResult
{
...
public static List sortResults(boolean ascending, List<CaseResult> toSort)
    {   ...
    }
}

I am getting the following error:

Name clash: The method sortResults(boolean, List) of type CaseResult has the same erasure as sortResults(boolean, List) of type QueryResult but does not hide it CaseResult.java

I tried answers under the similar question Java generics name clash , has the same erasure but got more errors. I think I may misunderstand something or those answers do not fit my case.

Could someone please provide any solutions or explain more to help me understand? Thank you all.

解决方案

As per Java Documentation,

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

The distinction between hiding a static method and overriding an instance method has important implications:

  • The version of the overridden instance method that gets invoked is the one in the subclass.
  • The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass

这篇关于Java名称冲突错误,一种方法与另一种方法具有相同的擦除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆