public class Team<AbstractMember> {
public int getPoints() {
int points = 0;
for (AbstractMember member : memberList) {
points += member.**getPoints()**;
}
return points;
}
}
我想遍历成员的要点并返回它们,但事实是AbstractMember是泛型类的类型,因此禁用了方法
member.getPoints()
。Eclipse说
The method getPoints() is undefined for the type AbstractMember
。public abstract class AbstractMember{
private String name;
private int points;
public int getPoints() {
return points;
}
}
如何使用泛型类的已定义类型的方法?
最佳答案
更改此声明
public class Team<AbstractMember>
至
public class Team<T extends AbstractMember>
然后,在代码中,应使用
T
引用类Type。例如,您将必须更改for
循环for (AbstractMember member : memberList)
至
for (T member : memberList) {