老师请见正文,这里哒不下这么多字~
我先把我整个我觉得正确的代码放上来~
package com.imooc.model;
/**
* Staff员工类
*/
public class Staff {
// 成员属性:姓名、工号、年龄、性别、所属部门、职务信息
private String staffName;
private String staffNo;
private int staffAge;
private String staffSex;
private Department staffDepartment; // 将Department作为员工的成员属性存在
private Position staffPosition; //// 将Position作为员工的成员属性存在
// 无参构造方法
public Staff() { //this()
this(null,null,18,"男",null,null);
/*上面的相当于下面的简化版
this.setStaffNo(null);
this.setStaffAge(18);
this.setStaffSex("男");
this.setStaffDepartment(null);
this.setStaffPosition(null);*/
}
/**
* 多参构造方法,实现对全部属性赋值
* @param staffName 员工姓名
* @param staffNo 员工工号
* @param staffAge 员工年龄
* @param staffSex 员工性别
* @param department 所属部门
* @param position 职务信息
*/
public Staff(String staffName, String staffNo, int staffAge, String staffSex, Department staffDepartment,
Position staffPosition) {
this.setStaffName(staffName);
this.setStaffNo(staffNo);
this.setStaffAge(staffAge);
this.setStaffSex(staffSex);
this.setStaffDepartment(staffDepartment);
this.setStaffPosition(staffPosition);
}
/**
* 多参构造方法,实现对员工姓名、工号、年龄、性别以及职务信息的赋值(符合题目要求)
* @param staffName
* @param staffNo
* @param staffSex
* @param staffAge
* @param staffPosition
*/
public Staff(String staffName, String staffNo, int staffAge, String staffSex, Position staffPosition) {
this(staffName,staffNo,staffAge,staffSex,null,staffPosition);
}
// 设置属性的set/get方法
public void setStaffName(String staffName) {
this.staffName = staffName;
}
public String getStaffName() {
return this.staffName;
}
public void setStaffNo(String staffNo) {
this.staffNo = staffNo;
}
public String getStaffNo() {
return staffNo;
}
/**
* 给年龄赋值,限定在18-65之间,否则默认为18
* @param staffAge 传入的年龄
*/
public void setStaffAge(int staffAge) {
this.staffAge = staffAge;
}
public int getStaffAge() {
if (staffAge < 18 || staffAge > 65) {
this.staffAge = 18;
} else {
this.staffAge = staffAge;
}
return this.staffAge;
}
/**
* 给性别赋值,设定性别只能为“男”或“女”,否则默认为“男”
* @param staffSex
*/
public void setStaffSex(String staffSex) {
this.staffSex = staffSex;
}
public String getStaffSex() {
if (staffSex=="女"){
this.staffSex = "女";
} else {
this.staffSex = "男";
}
return this.staffSex;
}
public void setStaffDepartment(Department staffDepartment) {
this.staffDepartment = staffDepartment;
}
/**
* 获取Department对象,如果没有实例化,先实例化然后返回
* @return Department对象信息
*/
public Department getStaffDepartment() {
if (this.staffDepartment == null) {
this.staffDepartment = new Department();
}
return staffDepartment;
}
public void setStaffPosition(Position staffPosition) {
this.staffPosition = staffPosition;
}
/**
* 获取Position对象,如果没有实例化,先实例化然后返回
* @return Position对象信息
*/
public Position getStaffPosition() {
if (this.staffPosition == null) {
this.staffPosition = new Position();
}
return staffPosition;
}
/**
* 员工自我介绍的方法
*
* @return 员工自我介绍的信息,包括姓名、工号、年龄、性别以及职务类中的职务名称
*/
public String introduction() {
String str = "姓名:" + this.getStaffName() + "\n工号:" + this.getStaffNo() + "\n性别:"
+ this.getStaffSex() + "\n年龄:" + this.getStaffAge() + "\n职务:"
+ this.getStaffDepartment().getDepartmentName() + this.getStaffPosition().getPositionName();
return str;
}
}
package com.imooc.model;
/**
* Department部门类
*/
public class Department {
// 成员属性:部门编号、部门名称、员工数组、统计部门中的员工个数
private String departmentNo;
private String departmentName;
private Staff[] myStaffs;
private int staffNum;
// 无参构造方法 +自己设定的默认值
public Department() {
this(null, null, null,0);
}
/**
* 多参构造方法,实现对全部属性赋值
* @param departmentNo 部门编号
* @param departmentName 部门名称
* @param myStaffs 员工数组
* @param staffNum 统计部门中员工的人数
*/
public Department(String departmentNo, String departmentName, Staff[] myStaffs, int staffNum) {
this.setDeparmentNo(departmentNo);
this.setDepartmentName(departmentName);
this.setMyStaffs(myStaffs);
this.getStaffNum(); //因为staffNum不是我定义的,所以我感觉这里用get比较好,我把set方法去掉了
}
/**
* 单一参数构造方法,实现对Department部门名称的赋值(符合题目要求)
*
* @param departmentName部门名称
*/
public Department(String departmentName) {
this(null, departmentName, null,0);
}
// 设置属性的set/get方法
public void setDeparmentNo(String departmentNo) {
this.departmentNo = departmentNo;
}
public String getDepartmentNo() {
return this.departmentNo;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getDepartmentName() {
return this.departmentName;
}
public void setMyStaffs(Staff[] myStaffs) {
this.myStaffs = myStaffs;
}
/**
* 获取部门内的员工信息,如果保存员工的信息数组未被初始化,则先初始化长度为200
*
* @return 保存的员工数组信息
*/
public Staff[] getMyStaffs() {
if (this.myStaffs == null) {
this.myStaffs = new Staff[10];
}
return myStaffs;
}
public int getStaffNum() {
return this.staffNum;
}
/**
* 将输入的值保存到数组中的方法 1.将员工保存到数组中 2.将员工个数保存在staffNum中
*/
public void addStaff(Staff sta) {
for(int i=0; i<this.getMyStaffs().length;i++) {
if(this.getMyStaffs()[i]==null) {
sta.setStaffDepartment(this);
this.getMyStaffs()[i] = sta;
this.staffNum = i+1;
return;
}
}
}
}
package com.imooc.model;
/**
* Position职务类
*/
public class Position {
// 成员属性:
private String positionNo;
private String positionName;
//无参构造方法
public Position() {
this(null,null);
}
/**
* 多参构造方法,实现对全部属性进行赋值
* @param positionNo 职务编号
* @param positionName 职务名称
*/
public Position(String positionNo, String positionName) {
this.setPositionNo(positionNo);
this.setPositionName(positionName);
}
/**
* 单参构造方法,实现对职务名称的赋值(符合题目要求)
* @param positionName 职务名称
*/
public Position(String positionName) {
this(null,positionName);
}
// 设置属性的set/get方法
public void setPositionNo(String positionNo) {
this.positionNo = positionNo;
}
public String getPositionNo() {
return this.positionNo;
}
public void setPositionName(String positionName) {
this.positionName = positionName;
}
public String getPositionName() {
return this.positionName;
}
}
package com.imooc.test;
import com.imooc.model.Staff;
import com.imooc.model.Department;
import com.imooc.model.Position;
public class Test {
public static void main(String[] args) {
Position p1 = new Position("经理");
Position p2 = new Position("助理");
Position p3 = new Position("职员");
Department d1 = new Department("人事部");
Department d2 = new Department("市场部");
Staff sta1 = new Staff("张铭", "S001", 29, "男", p1);
d1.addStaff(sta1);
Staff sta2 = new Staff("李艾爱", "S002", 21, "女", p2);
d1.addStaff(sta2);
Staff sta3 = new Staff("孙超", "S003", 29, "男", p3);
d1.addStaff(sta3);
Staff sta4 = new Staff("张美美", "S004", 26, "女", p3);
d2.addStaff(sta4);
Staff sta5 = new Staff("蓝迪", "S005", 37, "男", p1);
d2.addStaff(sta5);
Staff sta6 = new Staff("米莉", "S006", 24, "女", p3);
d2.addStaff(sta6);
System.out.println("===================================");
System.out.println(sta1.introduction());
System.out.println("===================================");
System.out.println(sta2.introduction());
System.out.println("===================================");
System.out.println(sta3.introduction());
System.out.println("===================================");
System.out.println(sta4.introduction());
System.out.println("===================================");
System.out.println(sta5.introduction());
System.out.println("===================================");
System.out.println(sta6.introduction());
System.out.println("===================================");
System.out.println(d1.getDepartmentName() + "总共有" + d1.getStaffNum() + "名员工");
System.out.println(d2.getDepartmentName() + "总共有" + d2.getStaffNum() + "名员工");
}
}
问题1:我想不用for循环,用数组a[i].length代替属性staffnum,应该怎么实现呢?
我原本想用这个:
public int addStaffNum(Staff sta){
sta.setStaffDeparment(this);
int i = this.getMyStaff().length;
this.getMyStaff()[i] = sta;
return i+1;
结果发现前面已经定义了数组长度,简直吐血。。。
public Staff[] getMyStaffs() {
if (this.myStaffs == null) {
this.myStaffs = new Staff[10];
}
return myStaffs;
}
请问老师有可以不用声明的方法来操作吗?
问题2:用this()代替构造方法中的内容这个做法叫做什么呀?是属于方法重载吗?
问题3:我为什么想用数组a[i].length代替属性staffnum是因为,如果我们都知道有多少人了,那直接在staffnum里定义数值就好了呀,我觉得setstaffnum这个方法数多余的,所以我没写。41
收起
正在回答
1回答
同学你好,1、同学所说的方法并不能实现,因为在数组的声明中,如果只定义不赋值就必须要指定长度。
2、this()表示当前对象。this(null, null, null,0); 是设置元素的默认值,但是成员方法有默认值,可以不设置。
3、现阶段是数组,必须指定长度,在后面学习了集合,就可以不指定长度了。
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9410 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星