关于两个对象调用一个方法的变量累计问题
package cc.grgroup.entity; public class Staff { private String staffName; private String staffNum; private int staffAge; private String staffSex; private Department staffDepartmant; private Position staffPosition; public String getStaffName() { return staffName; } public void setStaffName(String staffName) { this.staffName = staffName; } public String getStaffNum() { return staffNum; } public void setStaffNum(String staffNum) { this.staffNum = staffNum; } public int getStaffAge() { return staffAge; } public void setStaffAge(int staffAge) { if(staffAge>=18&&staffAge<=65){ this.staffAge = staffAge; }else{ this.staffAge = 18; } } public String getStaffSex() { return staffSex; } public void setStaffSex(String staffSex) { if(staffSex.equals("男")||staffSex.equals("女")){ this.staffSex = staffSex; }else{ this.staffSex = "男"; } } public Department getStaffDepartmant() { return staffDepartmant; } public void setStaffDepartmant(Department staffDepartmant) { this.staffDepartmant = staffDepartmant; } public Position getStaffPosition() { return staffPosition; } public void setStaffPosition(Position staffPosition) { this.staffPosition = staffPosition; } public Staff(){} public Staff(String staffName,String staffNum,int staffAge,String staffSex,Department staffDepartmant,Position staffPosition){ this.setStaffName(staffName); this.setStaffNum(staffNum); this.setStaffAge(staffAge); this.setStaffSex(staffSex); this.setStaffDepartmant(staffDepartmant); this.setStaffPosition(staffPosition); } public String staffShow(){ String str = "姓名:"+this.getStaffName()+"\n工号:"+this.getStaffNum()+"\n性别:"+this.getStaffSex()+"\n年龄:"+this.getStaffAge()+"\n职务:"+this.getStaffDepartmant().getDepartmentName()+this.getStaffPosition().getPositionName(); return str; } } package cc.grgroup.entity; public class Department { private int departmentNum; private String departmentName; private Staff[] departmentStaff = new Staff[10]; private int departmentStaffCoun; public int getDepartmentNum() { return departmentNum; } public void setDepartmentNum(int departmentNum) { this.departmentNum = departmentNum; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } public Staff[] getDepartmentStaff() { return departmentStaff; } public void setDepartmentStaff(Staff[] departmentStaff) { this.departmentStaff = departmentStaff; } public int getDepartmentStaffCoun() { return departmentStaffCoun; } public void setDepartmentStaffCoun(int departmentStaffCoun) { this.departmentStaffCoun = departmentStaffCoun; } public Department(){} public Department(int departmentNum,String departmentName){ this.setDepartmentName(departmentName); this.setDepartmentNum(departmentNum); } public String departmentShow(){ String str ="部门编号:"+this.getDepartmentNum()+"\n部门名称:"+this.getDepartmentName(); return str; } public void addDepartmentStaff(Staff staff){ for(int i=0;i<this.getDepartmentStaff().length;i++){ if(this.getDepartmentStaff()[i] == null){ this.getDepartmentStaff()[i] = staff; this.setDepartmentStaffCoun(getDepartmentStaffCoun()+1); return; } } } } package cc.grgroup.entity; public class Position { private int positionNum; private String positionName; public int getPositionNum() { return positionNum; } public void setPositionNum(int positionNum) { this.positionNum = positionNum; } public String getPositionName() { return positionName; } public void setPositionName(String positionName) { this.positionName = positionName; } public Position(){} public Position(int positionNum,String positionName){ this.setPositionNum(positionNum); this.setPositionName(positionName); } public String positionShow(){ String str = "职务编号:"+this.getPositionNum()+"职务名称:"+this.getPositionName(); return str; } } package cc.grgroup.test; import cc.grgroup.entity.Department; import cc.grgroup.entity.Position; import cc.grgroup.entity.Staff; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Department d1 = new Department(1, "人事部"); Department d2 = new Department(2, "市场部"); Position p1 =new Position(1, "经理"); Position p2 =new Position(2, "助理"); Position p3 =new Position(3, "职员"); Staff s1 = new Staff("张铭", "S001", 23, "男", d1, p1); System.out.println(s1.staffShow()); System.out.println("============================="); Staff s2 = new Staff("李艾爱", "S002", 21, "女", d1, p2); System.out.println(s2.staffShow()); System.out.println("============================="); Staff s3 = new Staff("孙超", "S003", 29, "男", d1, p3); System.out.println(s3.staffShow()); System.out.println("============================="); Staff s4 = new Staff("张美美", "S004", 26, "女", d2, p3); System.out.println(s4.staffShow()); System.out.println("============================="); Staff s5 = new Staff("蓝迪", "S005", 37, "男", d2, p1); System.out.println(s5.staffShow()); System.out.println("============================="); Staff s6 = new Staff("米莉", "S006", 24, "女", d2, p3); System.out.println(s6.staffShow()); System.out.println("============================="); d1.addDepartmentStaff(s1); d1.addDepartmentStaff(s2); d2.addDepartmentStaff(s4); d1.addDepartmentStaff(s3); d2.addDepartmentStaff(s4); d2.addDepartmentStaff(s5); d2.addDepartmentStaff(s6); System.out.println("人事部共"+d1.getDepartmentStaffCoun()+"人\n"+"市场部共"+d2.getDepartmentStaffCoun()+"人"); } }
1
收起
正在回答
2回答
同学你好,这里因为封装后的部门,实例化一个部门对象,在内存中开辟一块空间,通过调用添加员工的方法,改变这个部门对象中的员工数组和员工人数的属性值!比如:
通过人事部的部门对象,只能修改人事部对象中的成员属性的值哦!
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星