I've read the other questions related to erasures, but I'm still not clear why I get the compile error in the class below. The other questions involve methods that actually use the generic type, whereas I'm just trying to implement a method using the exact same signature. Can anyone explain?
我已经阅读了与删除有关的其他问题,但是我仍然不清楚为什么我在下面的类中会得到编译错误。其他问题涉及实际使用泛型类型的方法,而我只是试图使用完全相同的签名实现方法。谁能解释?
Compile error -> name *: bar(java.util.Set) in test.Baz and bar(java.util.Set) in test.Foo have the same erasure, yet neither overrides the other
编译错误->名称冲突:bar(java.util.Set)在测试中。Baz和bar(java.util.Set)在测试中。Foo有相同的擦除,但不覆盖另一个
import java.util.Set;
public class test {
public interface Foo<T> {
public void bar(Set<String> s);
}
public abstract class Baz implements Foo {
public void bar(Set<String> s) {}
}
}
2 个解决方案
#1
15
Did you intend for Baz to implement Foo<String>
or to implement Foo<T>
or to implement Foo
? What you typed was the last of those, but this means that Bar implements the "ungenerified" Foo. The weird thing is that this turns off generics in Foo
's definition entirely, not just the generics that have to do with T
.
您是否打算让Baz实现Foo
That means that inside Baz
, the method Foo.bar
takes a parameter of just Set
, not Set<String>
.
这意味着在Baz内,方法Foo。bar接受一个刚刚设置的参数,而不是设置
You want either:
你想要的:
public abstract class Baz implements Foo {
public void bar(Set s) {}
}
or
或
public abstract class Baz<T> implements Foo<T> {
public void bar(Set<String> s) {}
}
or
或
public abstract class Baz implements Foo<Void> {
public void bar(Set<String> s) {}
}
Depending on what you're trying to do.
这取决于你要做什么。
#2
4
I think you need to change this:
我认为你需要改变这一点:
public abstract class Baz implements Foo {
to this:
:
public abstract class Baz implements Foo<T> {
That way you properly override function Foo
.
这样就可以正确地覆盖函数Foo。
#1
15
Did you intend for Baz to implement Foo<String>
or to implement Foo<T>
or to implement Foo
? What you typed was the last of those, but this means that Bar implements the "ungenerified" Foo. The weird thing is that this turns off generics in Foo
's definition entirely, not just the generics that have to do with T
.
您是否打算让Baz实现Foo
That means that inside Baz
, the method Foo.bar
takes a parameter of just Set
, not Set<String>
.
这意味着在Baz内,方法Foo。bar接受一个刚刚设置的参数,而不是设置
You want either:
你想要的:
public abstract class Baz implements Foo {
public void bar(Set s) {}
}
or
或
public abstract class Baz<T> implements Foo<T> {
public void bar(Set<String> s) {}
}
or
或
public abstract class Baz implements Foo<Void> {
public void bar(Set<String> s) {}
}
Depending on what you're trying to do.
这取决于你要做什么。
#2
4
I think you need to change this:
我认为你需要改变这一点:
public abstract class Baz implements Foo {
to this:
:
public abstract class Baz implements Foo<T> {
That way you properly override function Foo
.
这样就可以正确地覆盖函数Foo。