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) {

07-26 04:20