作业题中的问题
public void setEmployeeAge(int employeeAge) {
if ((this.employeeAge < 18) || this.employeeAge > 65)
this.employeeAge = 18;
else
this.employeeAge=employeeAge;
}
public String getEmployeeSex() {
return employeeSex;
}
public void setEmployeeSex(String employeeSex) {
if (!(this.equals("男") && this.equals("女")))
this.employeeSex ="男";
else
this.employeeSex=employeeSex;
}
请问我这样写限定条件有什么错误,为什么无法正确限定??37
收起
正在回答
4回答
public void setEmployeeAge(int employeeAge) {
if ((employeeAge < 18) || employeeAge > 65)
this.employeeAge=18;
else
this.employeeAge=employeeAge;
}
public String getEmployeeSex() {
return employeeSex;
}
public void setEmployeeSex(String employeeSex) {
if (!(employeeSex.equals("男") && employeeSex.equals("女")))
this.employeeSex ="男";
else
this.employeeSex=employeeSex;
}
慕粉15719717264
2018-03-25 17:42:10
public class Student {
private int employeeAge;
private String employeeSex;
public Student() {}
public Student(int employeeAge,String employeeSex) {
this.setEmployeeAge(employeeAge);
this.setEmployeeSex(employeeSex);
}
//set设置信息:
public void setEmployeeAge(int employeeAge) {
if ( employeeAge<18 || employeeAge>65) //这块去掉this
this.employeeAge=18;
else
this.employeeAge=employeeAge;
}
public void setEmployeeSex(String employeeSex) {
if (!(employeeSex.equals("男") || employeeSex.equals("女")))
this.employeeSex ="男";
else
this.employeeSex=employeeSex;
}
//get活得信息:
public int getEmployeeAge() {
return this.employeeAge;
}
public String getEmployeeSex() {
return this.employeeSex;
}
}//这是完整的语句,性别那块的原因是用了与(&&)的关系,用或(||)的关系就正确了。
慕粉15719717264
2018-03-25 17:03:38
the__sky123
2018-03-25 16:53:35
为什么在set中的if中添加this 就不能正常限定
而如果把限定条件加到get中无论有无this都可以正常限定??
相似问题
登录后可查看更多问答,登录/注册
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星