Java泛型名称冲突,方法未被正确覆盖 [英] Java Generics name clash, method not correctly overridden

查看:536
本文介绍了Java泛型名称冲突,方法未被正确覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾看到有关此问题的不同问题,但我仍然觉得这个话题非常混乱。



我想要做的是拥有一个抽象类,它实现一个接口,并有一个扩展这个抽象类的类,这样硬类需要实现 getKommune() setKommune(Kommune kommune),但不是其他方法,因为它在抽象类中。



我有以下界面。

  public interface KommuneFilter {

< E extends AbstractKommune<>> void addKommuneFromCurrentUser(E e);

Kommune getKommune();

void setKommune(Kommune kommune);
}

和这个Abstract class

  public abstract class AbstractKommune< E extends AbstractKommune<>>实现KommuneFilter {
@PrePersist
void addKommuneFromCurrentUser(E e){
Kommune k = e.getKommune();
}
}

我想这样使用它

  public class Person extends AbstractKommune< Person> {
私营公社公社;
public void setKommune(Kommune kommune){this.kommune = kommune;}
public Kommune getKommune(){return kommune;}
}

然而,我得到

名字冲突:方法与类型擦除相同但不覆盖它



为什么不能正确覆盖?



UPDATE
感谢@Bozho,解决方案是这样的:

  public interface KommuneFilter< E extends AbstractKommune<>> {
public void addKommuneFromCurrentUser(E e);
}

public abstract class AbstractKommune< E extends AbstractKommune<>>实现KommuneFilter< E>

public class Person extends AbstractKommune< Person>


解决方案

只有它的方法:

  interface KommuneFilter< E extends AbstractKommune<>> {..} 

然后

  public abstract class AbstractKommune< E extends AbstractKommune<>> 
实现了KommuneFilter< E>


I have seen different questions regarding this, but I still find this topic to be very confusing.

All I want to do, is have an abstract class that implements an interface, and have a class extending this abstract class so that the hard class needs to implement getKommune() and setKommune(Kommune kommune), but not the other method, because that is in the abstract class.

I have the following interface.

public interface KommuneFilter {

    <E extends AbstractKommune<?>> void addKommuneFromCurrentUser(E e);

    Kommune getKommune();

    void setKommune(Kommune kommune);
}

And this Abstract class

public abstract class AbstractKommune<E extends AbstractKommune<?>> implements KommuneFilter {
    @PrePersist
    void addKommuneFromCurrentUser(E e) {
            Kommune k = e.getKommune();
    }
}

And I want to use it like this

    public class Person extends AbstractKommune<Person> {
        private Kommune kommune;             
        public void setKommune(Kommune kommune) {this.kommune=kommune;}
        public Kommune getKommune() {return kommune;}
    }

However, I get

Name clash: The method of has the same erasure of type but does not override it

Why isn't it correctly overridden?

UPDATE

Thanks to @Bozho, the solution is this:

public interface KommuneFilter<E extends AbstractKommune<?>> {
    public void addKommuneFromCurrentUser(E e);
}

public abstract class AbstractKommune<E extends AbstractKommune<?>> implements KommuneFilter<E> 

public class Person extends AbstractKommune<Person>

解决方案

I'd suggest making the interface generic, rather than only its method:

interface KommuneFilter<E extends AbstractKommune<?>> { .. }

And then

public abstract class AbstractKommune<E extends AbstractKommune<?>> 
     implements KommuneFilter<E> 

这篇关于Java泛型名称冲突,方法未被正确覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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