简体   繁体   English

名称冲突:类型为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

import java.util.*; import java.util。*;

  class A extends HashSet<Integer> {
       public boolean add(Object obj){  //compiler error
        return true;
       }
    }
    or
   class Abc <T> {
    public void add(T t){}  //compiler error
    public void add(Object i){} //compiler error (can't overload?)
    }

Error:Name clash: The method add(Object) of type test2 has the same erasure as add(E) of type HashSet but does not override it 错误:名称冲突:类型为test2的方法add(Object)与类型为HashSet的add(E)具有相同的擦除但不覆盖它

i do not know what is the concept behind above error can any one suggest where i can study this concept? 我不知道上面背后的概念是什么错误可以任何人建议我可以在哪里研究这个概念?

The concept at work here is called type erasure . 这里的概念称为类型擦除 HashSet defines a method add(T) , and you define a method add(Object) . HashSet定义了一个方法add(T) ,并定义了一个方法add(Object) At a glance one might think this is OK; 人们可能会认为这是可以的; that your method just overloads add . 您的方法只是重载add However, the erasure of T is Object and so the two have the same erased signature. 然而, T的擦除是Object ,因此两者具有相同的擦除签名。

Now, that would be fine if your method properly overrode the method from HashSet . 现在,如果你的方法从HashSet正确地覆盖了方法,那HashSet But to do so you should be using add(Integer) and not add(Object) . 但要这样做,你应该使用add(Integer)而不是add(Object) You're not properly overriding the parent method, so instead it is reported as a conflict since a class cannot provide two methods with the same signature. 您没有正确覆盖父方法,因此它被报告为冲突,因为类不能提供具有相同签名的两个方法。

Your Abc example follows the same reasoning. 您的Abc示例遵循相同的推理。 The two methods you declared have the same erased signature so they clash. 您声明的两个方法具有相同的擦除签名,因此它们会发生冲突。

Further Reading 进一步阅读

Angelika Langer Generics FAQ Angelika Langer Generics FAQ

interface CollectionConverter<U> {
    <T> List<T> toList(Collection<T> c);

    void fooMethod(Class<?> c);

    <E>Comparable<E> method3(E e);

    Comparable<U> method4(U u);
}

class Overrider implements CollectionConverter<Integer> {
    @Override
    public List toList(Collection c) {
        return null;
    }

    @Override
    public void fooMethod(Class c) {

    }

    @Override
    public  Comparable method3(Object o) {
        return null;
    }

    @Override
    public Comparable method4(Integer u) {
        return null;
    }
}

This code works well. 这段代码效果很好。 In JLS: The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other. 在JLS中:子签名的概念旨在表达两个方法之间的关系,这两个方法的签名不相同,但其中一个方法可能会覆盖另一个方法。 Specifically, it allows a method whose signature does not use generic types to override any generified version of that method. 具体来说,它允许其签名不使用泛型类型的方法覆盖该方法的任何泛化版本。 This is important so that library designers may freely generify methods independently of clients that define subclasses or subinterfaces of the library. 这很重要,因此库设计者可以独立于定义库的子类或子接口的客户端自由地生成方法。

Did you try using Integer instead of Object obj ie 您是否尝试使用Integer而不是Object obj ie

public boolean add(Integer i) { //compiler error return true; }

The problem is that when you are extending Hashset , you are extending Integer Hashset and not the generic form. 问题是,当您扩展Hashset时,您将扩展Integer Hashset而不是泛型形式。 So, in the subclass your add method has to comply with the signature of the superclass method which is 因此,在子类中,add方法必须符合超类方法的签名

public boolean add(Integer i) { } public boolean add(Integer i){}

If you want to extend from a totally generic Hashset implementation, try extending with 如果您想从完全通用的Hashset实现扩展,请尝试使用

public class MyHashset extends Hashset<?> {

} }

Then your add method should work with Object. 然后你的add方法应该与Object一起使用。

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

相关问题 类型分区器的getPartition的名称冲突在MapReduce,Hadoop中具有与类型主类相同的擦除 - Name clash of getPartition of type Partitioner has the same erasure of type main class in MapReduce, Hadoop Java名称冲突错误,一种方法与另一种方法具有相同的擦除 - Java name clash error, a method has the same erasure as another method Java泛型名称冲突,具有相同的擦除 - Java generics name clash , has the same erasure 方法与类型中的另一个方法具有相同的擦除 - Method has the same erasure as another method in type 名称冲突,覆盖失败,在实现具有相同擦除的两个接口的类上 - Name Clash, override fail, on a class implementing two interfaces with same erasure 由于相同的擦除而命名冲突 - Name clash because of same erasure 为什么我的方法覆盖会产生类型冲突? - Why does my method override give a type clash? 方法与类型中的另一种方法具有相同的擦除 - 第2部分 - Method has the same erasure as another method in type - part 2 方法的擦除与类型中的另一个方法相同 - Erasure of method is the same as another method in type 如何解决名称冲突有相同的擦除? - How to solve name clash have the same erasure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM