简体   繁体   English

Java名称冲突错误,一种方法与另一种方法具有相同的擦除

[英]Java name clash error, a method has the same erasure as another method

I have two classes as follows 我有两个班,如下

class QueryResult: QueryResult类:

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

and class CaseResult: 和类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 名称冲突:CaseResult类型的方法sortResults(boolean,List)与QueryResult类型的sortResults(boolean,List)具有相同的擦除,但不会隐藏CaseResult.java

I tried answers under the similar question Java generics name clash , has the same erasure but got more errors. 我在Java泛型名称clash的类似问题下尝试了答案尽管擦除相同,但出现了更多错误。 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. 谢谢你们。

The reason for the error is covered in Darshit Chokshi's answer. 该错误的原因已在Darshit Chokshi的答案中涵盖。 However, since a solution/alternative has not been posted yet, try the following: 但是,由于尚未发布解决方案/替代方法,请尝试以下方法:

You are trying to override the sortResults() method in such a way so that you can sort lists with different elements. 您试图以这种方式覆盖sortResults()方法,以便可以对具有不同元素的列表进行排序。 However, when overriding you need to have the same type signature. 但是,覆盖时,您需要具有相同的类型签名。 The signature looks similar in your case, but since the elements in the List differ - the compiler realises List< QueryResult> and List< CaseResult> is different, but due to type erasure in Java, it would be uncertain which method it should be calling - ie should it call the super class method or the subclass method. 在您的情况下,签名看起来相似,但是由于List中的元素不同-编译器意识到List <QueryResult>和List <CaseResult>是不同的,但是由于Java中的类型擦除,因此无法确定应该调用哪种方法-即应调用超类方法还是子类方法。

Instead of overriding, rather change the original method in the super class (in your case, QueryResult) so that it can handle anytype of List element. 而不是覆盖,而是更改超类(在您的情况下为QueryResult)中的原始方法,以便它可以处理任何类型的List元素。 You can use wildcard capturing to accomplish this: 您可以使用通配符捕获来完成此操作:

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

This method receives a list and will infer the element type, assigning the element type to the generic type parameter T. Whenever you then want to refer to the element type in the body of the method, instead of using the element type name (previously either QueryResult or CaseResult), now you would use the generic type variable T instead. 此方法将接收一个列表,并将推断元素类型,并将元素类型分配给通用类型参数T。每当您随后要在方法主体中引用元素类型时,都不要使用元素类型名称(以前是QueryResult或CaseResult),现在您将改用通用类型变量T。

You can also put further bounds on this variable if required (ie if the list element needs to be a subtype of QueryResult you can say < T extends QueryResult>), this will force Java to do a type check on the List and what type of elements it may have. 如果需要,您还可以在此变量上进一步限制(即,如果list元素需要是QueryResult的子类型,则可以说<T扩展QueryResult>),这将强制Java对List进行类型检查以及哪种类型的它可能具有的元素。

A further comment on your original code. 对您的原始代码的进一步评论。 Be very careful of using raw types in new generic code - you are returning a list as a raw type: List, without specifying what the element type of the list is ie a parameterised type: List< SomeActualType>. 在新的通用代码中使用原始类型时要非常小心-您将返回一个列表作为原始类型:List,但未指定列表的元素类型是什么,即,参数化类型:List <SomeActualType>。 This can cause many problems and is not advised. 这可能会导致许多问题,因此不建议这样做。 The Java creators kept this form of coding in Java for backward compatibility of code that existed before generics, but it is strongly advised not to code in this manner for new written code. Java创建者将这种形式的代码保留在Java中,以实现通用代码之前存在的代码的向后兼容性,但是强烈建议不要以这种方式对新编写的代码进行编码。

You can read up more about the raw type vs parameterised type and the pitfalls of using raw types, as well as more information on wildcard capturing and bounds, in the textbook: 您可以在教科书中详细了解原始类型与参数化类型以及使用原始类型的陷阱,以及有关通配符捕获和范围的更多信息:

Effective Java, by Joshua Bloch
    Section: Generics
    Item 23: Don't use raw types in new code
    Item 28: Use bounded wildcards to increase API flexibility

As per Java Documentation , 根据Java文档

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 调用的隐藏静态方法的版本取决于是从超类还是从子类调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java泛型名称冲突,具有相同的擦除 - Java generics name clash , has the same erasure 方法与类型中的另一个方法具有相同的擦除 - Method has the same erasure as another method in type 名称冲突:类型为test2的方法add(Object)具有与HashSet类型的add(E)相同的擦除 <E> 但不会覆盖它 - Name clash: The method add(Object) of type test2 has the same erasure as add(E) of type HashSet<E> but does not override it 方法的擦除与另一种方法相同 - Erasure of method is the same as another method 名称冲突 - 具有相同的擦除但在方法参数generic中不会覆盖另一个 - Name clash - have the same erasure yet neither overrides the other in method parameter generic 方法与类型中的另一种方法具有相同的擦除 - 第2部分 - Method has the same erasure as another method in type - part 2 由于相同的擦除而命名冲突 - Name clash because of same erasure SkuDetailsResponseListener() 中的“两种方法都具有相同的擦除功能,但都不会覆盖另一个”方法冲突错误 - 'Both methods have same erasure, yet neither overides the other' method clash error in SkuDetailsResponseListener() Java名称冲突,具有相同的擦除方式,但两者都不隐藏 - Java name clash, have the same erasure, neither hides the other 方法的擦除与类型中的另一个方法相同 - Erasure of method is the same as another method in type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM