java - 返回一个类对应的对象
问题描述
所以我有 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.
}
}
解决方案
推荐阅读
- python - 为什么性能会随着数据框的大小而降低?
- android - WebView 内部布局,除 Linearlayout 外具有百分比高度
- python-3.x - 使用 Python 3.6 将二维 rgb 矩阵转换为 PNG 图像
- angular - 如何自定义弹出框与其离子选择的宽度相同
- c# - 如何在 Unity webrequest 中使用公共访问令牌?
- javascript - 如何通过 Javascript 函数访问返回的对象?
- javascript - 如何使用分页(链接)php或js创建AZ索引
- mysql - 如何在 wp_posts 表的 guid 列中生成 URL?
- c# - 根据 ID 选择文件
- php - 通过子目录中的 htaccess 从 URL 中删除目录名称