题目内容
(7-16)根据提示补全程序空白处,使程序能够正确运行。//观察者模式案例:教师授课,学生听课//老师接口publicinterfaceITeacher{publicvoidaddStudent(IStudentstudent);//增加学生publicvoiddeleteStudent(IStudentstudent);//移除学生publicvoid①(Stringcontent);//讲授知识}//学生接口publicinterfaceIStudent{publicvoid②(Stringcontent);//学生听课}//老师类publicclassTeacherimplementsITeacher{//利用Vector保存所有的学生privateVector<③>studentList=newVector<>();privateStringname;//教师名publicTeacher(Stringname){this.name=name;}publicvoidaddStudent(IStudentstudent){this.studentList.add(student);}publicvoiddeleteStudent(IStudentstudent){this.studentList.remove(student);}//遍历老师的学生,告诉学生老师上课了publicvoidteaching(Stringcontent){System.out.println(this.name+"正在讲授:"+content+"\n");//老师给所有学生上课for(IStudentstudent:④){StringBuffersb=newStringBuffer();sb.append(((Student)student).getName()+"已经了解了"+content);System.out.println(sb.toString());}}}//学生类publicclassStudentimplementsIStudent{privateStringname;publicStudent(StringstudentName){this.name=studentName;}publicStringgetName(){returnname;}publicvoidlistening(Stringcontent){//学生在听课System.out.println(content);}}//测试类publicclassDemo{publicstaticvoidmain(String[]args){ITeacherts=newTeacher("唐僧");//产生一个老师IStudentswk=newStudent("孙悟空");//产生3个学生IStudentzbj=newStudent("猪八戒");IStudentshs=newStudent("沙和尚");//把学生加入到老师的课堂ts.addStudent(swk);ts.addStudent(zbj);ts.addStudent(shs);//老师开始上课ts.teaching("大乘佛教三藏真经");⑤;//猪八戒上课开小车,被唐僧赶出教室ts.teaching("徒弟们,现在吃东西");}}
查看答案
搜索结果不匹配?点我反馈