当我尝试重写一个需要List<String>
,我得到以下结果编译错误.
该行有多个标记:
- 方法getname(List<Integer>)
类型的child
必须重写或实现超类型方法
- 名称冲突:方法getname(List<Integer>)
类型的child
具有相同的擦除getname(List<String>)
类型的parent
但不会覆盖它
我的印象是,由于erasure,一种方法,需要List<String>
和一个子类方法,它采用List<Integer>
将被视为被覆盖,因为两种方法的签名在擦除后是相同的。
Here http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.2是的定义方法签名这涉及到erasure.
我不明白为什么会出现这个错误以及它到底意味着什么。
我的代码如下:
class parent {
public void getname(List<String> num) {
System.out.printf("parent class: %s",num);
}
}
class child extends parent {
@Override // here is the compile error
public void getname(List<Integer> num) {
System.out.printf("child class: %s",num);
}
}
List<String>
and List<Integer>
是不同的类型和getname(List<String> num)
and getname(List<Integer> num)
是具有不同签名的方法。所以第二个不会覆盖第一个。所以child
不能延伸parent
用这个方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)