首页 > 解决方案 > 返回一个类对应的对象

问题描述

所以我有 2 个班级,在班级比赛中我有一个方法( public Athlete getAthlete(int codAthlete) )应该返回与运动员对应的对象,并使用参数传递的代码,但我不知道如何实现它。有人可以帮帮我吗?

public class Athlete {
    private int codAthlete;
    private String name;

    public Athlete(int codAthlete){
        this.codAthlete = codAthlete;
    }

    public int getCodAthlete() {
        return this.codAthlete;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public String getInformation() {
    return "Code: " + this.codAthlete +
    " Name " + this.name;
    }
}

.

public class Race {
    private String idRace;
    private Set<Athlete> athletes;

    public Race(String idRace) {
        athletes = new HashSet<>();
        this.idRace = idRace;
    }

    public String getIdRace () {
        return this.idRace;
    }

    public Athlete getAthlete(int codAthlete){
        for(Athlete a: Athlete){
            if(a.getCodAthlete() == codAthlete)
                a.getInformation();
        }
        return (????);
         // Returns the object corresponding to the Athlete with the code passed by parameter.
    }
}

标签: java

解决方案


推荐阅读