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

问题描述

我有超级富豪。

  public class Bar extends Foo 

Foo中的函数:

  protected void saveAll(Collection   

Bar中的函数:

  public void saveAll(Collection< MyClass> stuff){
super.saveAll(stuff);

$ / code>

获取错误:

 名称冲突:Bar类型的方法saveAll(Collection< MyClass>)与Foo类型的saveAll(Collection<>)具有相同的擦除但不覆盖它。 

我做错了什么?

解决方案

您正在使用不兼容的类型重写 saveAll 方法。也许你想做类似的事情:

  public class Bar extends Foo< MyClass> 

函数 Foo< E> p>

  protected void saveAll(Collection< E> many)

和Bar中的函数:

  public void saveAll(Collection< MyClass> stuff) {
super.saveAll(stuff);
}


I have superclass Foo. And a class Bar extending it.

public class Bar extends Foo

Function in Foo:

protected void saveAll(Collection<?> many)

Function in Bar:

public void saveAll(Collection<MyClass> stuff) {
   super.saveAll(stuff);
}

Getting error :

 Name clash: The method saveAll(Collection<MyClass>) of type Bar has the same erasure as saveAll(Collection<?>) of type Foo but does not override it.

What am I doing wrong?

解决方案

You are overriding the saveAll method with an incompatible type. Perhaps you want to do something like:

public class Bar extends Foo<MyClass>

Function in Foo<E>

protected void saveAll(Collection<E> many)

and function in Bar:

public void saveAll(Collection<MyClass> stuff) {
   super.saveAll(stuff);
}

这篇关于Java泛型名称冲突,具有相同的擦除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:21