14.3 结合实例讲述如何设计封装
【实例14.4】本节通过这个综合的实例,让读者更加熟悉这种封装的程序设计思路。这个程序段是老师提出问题,让一个学生回答。先来了解这个程序的流程,如图14.1所示。
图 14.1 封装综合实例的流程图
首先建立学生类,代码如下所示。
01 ///创建一个学生回答问题类,并且让它
02 ///成为一个线程类
03 ///name指学生 姓名
04 ///age指学生的 年龄
05 ///school指学生所在学校
06 ///grade指学生 所在年级
07 ///year、month、days指年份、月份、日子
08 ///set是设置器
09 ///get是访问器
10 ///通过一个开关语句,来把几次不同的回答分成不同的情况
11 ///run方法是线程的运行方法
12 class studentanswer extends Thread
13 {
14 private String name;
15 private String age;
16 private String school;
17 private String grade;
18 private String year;
19 private String month;
20 private String days;
21 private int x;
22 public void set(String name, String age, String school, String grade, String year, String
23 month, String days)
24 {
25 this.name=name;
26 this.age=age;
27 this.school=school;
28 this.grade=grade;
29 this.year=year;
30 this.month=month;
31 this.days=days;
32 }
33 public void setint(int x)
34 {
35 this.x=x;
36 }
37 public String getname()
38 {
39 return name;
40 }
41 public String getage()
42 {
43 return age;
44 }
45 public String getschool()
46 {
47 return school;
48 }
49 public String getgrade()
50 {
51 return grade;
52 }
53 public String getyear()
54 {
55 return year;
56 }
57 public String getmonth()
58 {
59 return month;
60 }
61 public String getdays()
62 {
63 return days;
64 }
65 public int getint()
66 {
67 return x;
68 }
69 ///这是一个有关学生回答内容的方法
70 ///通过一个分支语句来控制回答的步骤
71 private void said()
72 {
73 switch(x)
74 {
75 case 0:
76 System.out.println(name+"说:我名字叫"+name+"。");
77 break;
78 case 1:
79 System.out.println(name+"说:我在"+school+"读书。");
80 break;
81 case 2:
82 System.out.println(name+"说:我现在在读"+grade+"。");
83 break;
84 case 3:
85 System.out.println(name+"说:我今年"+age+"岁。");
86 break;
87 case 4:
88 System.out.println(name+"说:我学习计算机软件开发"+year+"年。");
89 break;
90 case 5:
91 System.out.println(name+"说:今年"+month+"月放假。");
92 break;
93 case 6:
94 System.out.println(name+"说:一般放假的天数是"+days+"天。");
95 break;
96 case 7:
97 System.out.println(name+"说:不客气。");
98 break;
99 }
100 }
101 public void run()
102 {
103 said();
104 try
105 {
106 sleep(2000);}
107 catch(Exception e){}
108 }
109 }
再设计教师类,代码如下。
110 ///创建一个教师提问的线程类
111 ///name是指老师的姓名
112 ///set是设置器
113 ///get是访问器
114 ///通过一个开关语句,来把几次不同的回答分成不同的情况
115 ///run方法是线程的运行方法
116 class teacherask extends Thread
117 {
118 private String name;
119 private int x;
120 public void set(String name)
121 {
122 this.name=name;
123 }
124 public String getname()
125 {
126 return name;
127 }
128 public void setint(int x)
129 {
130 this.x=x;
131 }
132 public int getint()
133 {
134 return x;
135 }
136 ///这是一个老师提问的方法
137 ///通过分支语句来提供提问的步骤
138 private void said()
139 {
140 switch(x)
141 {
142 case 0:
143 System.out.println(name+"说:你叫什么名字?");
144 break;
145 case 1:
146 System.out.println(name+"说:你在哪所学校读书?");
147 break;
148 case 2:
149 System.out.println(name+"说:你读什么系哪一个年级?");
150 break;
151 case 3:
152 System.out.println(name+"说:你今年多大了?");
153 break;
154 case 4:
155 System.out.println(name+"说:你学习计算机软件开发几年了?");
156 break;
157 case 5:
158 System.out.println(name+"说:你几月放假?");
159 break;
160 case 6:
161 System.out.println(name+"说:放假大概有多少天?");
162 break;
163 case 7:
164 System.out.println(name+"说:谢谢你回答我的问题。");
165 break;
166 }
167 }
168 public void run() //让线程能够交叉运行
169 {
170 said();
171 try
172 {
173 sleep(2000);
174 }
175 catch(Exception e){}
176 }
177 }
最后通过主运行类,执行其对话的功能。
178 ///主运行类
179 ///sa1至sa8是学生回答类的八个对象
180 ///ta1至ta8是教师提问类的八个对象
181 ///stt[]是学生回答类对象集合
182 ///stt1是教师提问类对象集合
183 ///通过一个循环语句将对象集合中元素输出。并且是按线程运行方式交叉运行
184 public class thread45
185 {
186 public static void main(String[]args)throws Exception
187 {
188 studentanswer sa1=new studentanswer();//创建几个学生回答结果的对象
189 studentanswer sa2=new studentanswer();
190 studentanswer sa3=new studentanswer();
191 studentanswer sa4=new studentanswer();
192 studentanswer sa5=new studentanswer();
193 studentanswer sa6=new studentanswer();
194 studentanswer sa7=new studentanswer();
195 studentanswer sa8=new studentanswer();
196 teacherask ta1=new teacherask();//创建几个老师提问内容的对象
197 teacherask ta2=new teacherask();
198 teacherask ta3=new teacherask();
199 teacherask ta4=new teacherask();
200 teacherask ta5=new teacherask();
201 teacherask ta6=new teacherask();
202 teacherask ta7=new teacherask();
203 teacherask ta8=new teacherask();
204 studentanswer[]stt=new studentanswer[]{sa1,sa2,sa3,sa4,sa5,sa6,sa7,sa8};
205 teacherask[]stt1=new teacherask[]{ta1,ta2,ta3,ta4,ta5,ta6,ta7,ta8};
206 try
207 {
208 for(int x=0;x<8;x++)
209 {
210 stt[x].set("TOM","22","重庆大学","自动化系二年级","3","7","54");
211 stt1[x].set("John老师");
212 stt1[x].setint(x);
213 stt[x].setint(x);
214 stt1[x].start();
215 stt[x].start();
216 }
217 }
218 catch(Exception e){}
219 }
220 }
【代码说明】以上的程序段,使用了前面学过的知识:线程、分支语句、数组等。通过程序可以看到,封装后的所有方法和变量只能在程序的类的内部被使用,一旦走出了这个类,就是无效的。这也是前面章节所说的变量和方法的作用域。
【运行效果】
John老师说:你叫什么名字?
TOM说:我名字叫TOM。
John老师说:你在哪所学校读书?
TOM说:我在重庆大学读书。
John老师说:你读什么系哪一个年级?
TOM说:我现在在读自动化系二年级。
John老师说:你今年多大了?
TOM说:我今年22岁。
John老师说:你学习计算机软件开发几年了?
TOM说:我学习计算机软件开发3年。
John老师说:你几月放假?
TOM说:今年7月放假。
John老师说:放假大概有多少天?
TOM说:一般放假的天数是54天。
John老师说:谢谢你回答我的问题。
TOM说:不客气。