方法的擦除与另一种类型的方法相同 [英] Erasure of method is the same as another method in type
问题描述
类中的两个方法
class Developer and Student are different and not share any parent。 p>以下两个方法具有相同的代码和方法,这两个对象具有相同的名称ex。 getStartDate。
pre $ class $ hierarchy $ {$ b $ privateDispatcher {[$ b $ private] LocalDate [] [] [] getArrayOfTimespan(List< Developer> developers)
{
//这里我必须从列表
调用开发者的某些方法LocalDate startDate = developer.getStartDate();
....
}
private LocalDate [] [] getArrayOfTimespan(List< Student> students)
{
//这里我必须从列表
调用学生的某种方法LocalDate startDate = student.getStartDate();
.....
}
}
显示错误擦除方法与类型中的另一种方法相同
我了解同一网站上其他帖子发生错误的原因。
如何重新分解它以便错误不会出现在代码中并且代码将变得干净?
您的问题是由键入删除引起的:您的 List s中的参数化类型信息在运行时会被擦除,因此这些方法实际上具有相同的签名,您的代码无法编译。为了解决您的问题,下面是一个解决方案,它概括了
Developer
和 Student
,因此对于给定的参数化类型只需要一个 getArrayOfTimespan
方法:
//学生和开发人员类的通用接口
接口Datable {
LocalDate getStartDate();
}
// dev class实现datable
class开发者实现Datable {
@Override
public LocalDate getStartDate(){
// TODO Auto -generated方法存根
返回null;
//学生类实现datable
class Student实现Datable {
@Override
public LocalDate getStartDate(){
// TODO自动生成的方法存根
返回null;
//将参数解释为超类型的列表给开发人员和学生$ b $ private LocalDate [] [] getArrayOfTimespan(List< Datable> args)
{
for(Datable d:args){
// TODO something
LocalDate foo = d.getStartDate();
}
// TODO返回某物
返回null;
}
Following are two methods in a class
class Developer and Student are different and not share any parent.
Both following method has same code and method called on both object have same name ex. getStartDate.
class hierarchyValidator {
private LocalDate[][] getArrayOfTimespan(List<Developer> developers)
{
//here i have to call some method on developer from list
LocalDate startDate = developer.getStartDate();
....
}
private LocalDate[][] getArrayOfTimespan(List<Student> students)
{
//here i have to call some method on student from list
LocalDate startDate = student.getStartDate();
.....
}
}
It is showing error Erasure of method is the same as another method in type
I understand the reason behind error from other post on same site.
How to re-factored it so that error wont be there and code will be clean?
Your problem is due to type erasure: the parametrized type information in your List
s is erased at runtime, hence the methods have a virtually identical signature and your code cannot compile.
In order to solve your problem, here's a solution that generalizes the common features of Developer
and Student
, thus only requiring one getArrayOfTimespan
method for both given parametrized types:
// common interface to Student and Developer classes
interface Datable {
LocalDate getStartDate();
}
// dev class implementing "datable"
class Developer implements Datable {
@Override
public LocalDate getStartDate() {
// TODO Auto-generated method stub
return null;
}
}
// student class implementing "datable"
class Student implements Datable {
@Override
public LocalDate getStartDate() {
// TODO Auto-generated method stub
return null;
}
}
// parameter interpreted as list of super type to both dev and student
private LocalDate[][] getArrayOfTimespan(List<Datable> args)
{
for (Datable d: args) {
// TODO something
LocalDate foo = d.getStartDate();
}
// TODO return something
return null;
}
这篇关于方法的擦除与另一种类型的方法相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!