关于两个对象调用一个方法的变量累计问题

关于两个对象调用一个方法的变量累计问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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()+"人");
    }
 
}


http://img1.sycdn.imooc.com//climg/5d1db5a000018fce10430426.jpg

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

    同学你好,这里因为封装后的部门,实例化一个部门对象,在内存中开辟一块空间,通过调用添加员工的方法,改变这个部门对象中的员工数组和员工人数的属性值!比如:

http://img1.sycdn.imooc.com//climg/5d1dcfa9000137e709910648.jpg

通过人事部的部门对象,只能修改人事部对象中的成员属性的值哦!

        如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • qq_aiq款_hnrGLT 提问者 #1
    谢谢老师,终于搞明白了,笨死了,创建两个对象,每次改变变量是在不同对象的。
    2019-07-05 09:04:00
Robot大冰 2019-07-04 17:42:45

你把s4传了两次,多加了一次。

  • 提问者 qq_aiq款_hnrGLT #1
    传多一次没关系的吧?传多一次就统计多一次!
    2019-07-05 09:01:55
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码