// signatures of the reset method at //1 and //2 after erasure will be different
// then why don't they overload?
public class Test<T>{
public static void main(String[] args) {
WordBox<String> city = new WordBox<String>("Skogland");
city.reset("waiting"); // error: ambiguous
}
}
class Box <T> {
private T theThing;
public Box( T t) { theThing = t; }
public void reset( T t) { theThing = t; } //1
}
class WordBox< S extends CharSequence > extends Box< String > {
public WordBox( S t) { super(t.toString().toLowerCase()); }
public void reset( S t) { super.reset(t.toString().toLowerCase()); } //2
}
答案 0 :(得分:4)
java.lang.String
扩展了CharSequence
,因此调用city.reset("waiting")
会同时匹配WordBox.reset(S extends CharSequence)
和Box.reset(String)
。
要解决此问题,您应确保WordBox.reset()
接受与Box.reset()
相同的类型,在这种情况下WordBox.reset()
会覆盖Box.reset()
,或相反,请确保WordBox.reset()
{1}}接受与Word.reset()
不重叠的类型,在这种情况下WordBox.reset()
重载Box.reset()
。在您给出的示例中,我感觉您可能希望WordBox.reset()
覆盖Box.reset()
。