我的教授在setter中跳过了一个变量,他写的代码有错吗?
My professor skiped a variable in the setter, did he make a mistake in the code he wrote?
我们的教授给了我们练习,给了我们应该如何完成的代码,所以有些东西我不明白,我一周前给他发了一封电子邮件,但他没有回复,所以我转向 Whosebug,我没有在评论中得到:
public class Futbollisti {
private String emri, pozita;
private int mosha;
public Futbollisti(String e, String p, int m){
emri = e;
pozita = p;
mosha = m;
}
public String getEmri(){
return emri;
}
public String getPozita(){
return pozita;
}
public int getMosha(){
return mosha;
}
public void setPozita(String p){ /*right about here, why did he skip the
'emri', he went straight to pozita, but didn't set emri, did he do a
mistake? */
pozita = p;
}
public void setMosha(int m){
mosha = m;
}
public String toString(){
return emri + " : " + mosha + " - " + pozita;
}
public boolean equals(Object obj){
if(obj instanceof Futbollisti){
Futbollisti f = (Futbollisti) obj;
return emri.equals(f.getEmri())
&& pozita.equals(f.getPozita())
&& mosha == f.getMosha();
}
return false;
}
}
在练习中它说要创建一个接受这些变量(emri, pozita, mosha
)的构造函数,并初始化它们,然后为所需的属性提供 get 和 set 方法,并提供一个表示'Futbollisti'
class 对象中的字符串,格式为:'emri : mosha - pozita'
并且,提供一种比较 Futbollsti
的 2 个对象的方法,这些都已完成,但我不知道他是否应该遗漏 'emri'
。那么代码是正确的还是他弄错了?
Setter 如果开发人员想要将字段设置为只读,则省略方法。
根据您的需求描述:
make a constructor that accepts those variables (the emri, pozita,
mosha), and initialize them, then offer the get and set methods for
the attributes needed
这不太可能是只读字段,所以他忘记了 setter。
我们的教授给了我们练习,给了我们应该如何完成的代码,所以有些东西我不明白,我一周前给他发了一封电子邮件,但他没有回复,所以我转向 Whosebug,我没有在评论中得到:
public class Futbollisti {
private String emri, pozita;
private int mosha;
public Futbollisti(String e, String p, int m){
emri = e;
pozita = p;
mosha = m;
}
public String getEmri(){
return emri;
}
public String getPozita(){
return pozita;
}
public int getMosha(){
return mosha;
}
public void setPozita(String p){ /*right about here, why did he skip the
'emri', he went straight to pozita, but didn't set emri, did he do a
mistake? */
pozita = p;
}
public void setMosha(int m){
mosha = m;
}
public String toString(){
return emri + " : " + mosha + " - " + pozita;
}
public boolean equals(Object obj){
if(obj instanceof Futbollisti){
Futbollisti f = (Futbollisti) obj;
return emri.equals(f.getEmri())
&& pozita.equals(f.getPozita())
&& mosha == f.getMosha();
}
return false;
}
}
在练习中它说要创建一个接受这些变量(emri, pozita, mosha
)的构造函数,并初始化它们,然后为所需的属性提供 get 和 set 方法,并提供一个表示'Futbollisti'
class 对象中的字符串,格式为:'emri : mosha - pozita'
并且,提供一种比较 Futbollsti
的 2 个对象的方法,这些都已完成,但我不知道他是否应该遗漏 'emri'
。那么代码是正确的还是他弄错了?
Setter 如果开发人员想要将字段设置为只读,则省略方法。
根据您的需求描述:
make a constructor that accepts those variables (the emri, pozita, mosha), and initialize them, then offer the get and set methods for the attributes needed
这不太可能是只读字段,所以他忘记了 setter。