1.新建课程类:
public class Course {
private String coursename;//课程名称
public Course(String coursename) {
this.coursename = coursename;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
}
2.新建班级类:
public class Clazz {
private String clazzname;//班级名称
public Clazz(String clazzname) {
this.clazzname = clazzname;
}
public String getClazzname() {
return clazzname;
}
public void setClazzname(String clazzname) {
this.clazzname = clazzname;
}
}
3.新建教室类:
public class ClassRoom {
private String classroom;//地址
public ClassRoom(String classroom) {
this.classroom = classroom;
}
public String getClassroom() {
return classroom;
}
public void setClassroom(String classroom) {
this.classroom = classroom;
}
}
4.新建老师类:
public class Teacher {
private String name;//老师姓名
public Teacher(String name) {
this.name = name;
}
public void gotoClass(Course course, ClassRoom classRoom, Clazz clazz) throws Exception {
if(course!=null && classRoom !=null && clazz!=null){
StringBuilder str=new StringBuilder(80);
str.append("一个老师").append(name).append("到").append(classRoom.getClassroom()).append("教室");
str.append("给").append(clazz.getClazzname()).append("班级上").append(course.getCoursename());
System.out.println(str);
}else{
throw new Exception(name+"老师不上课!");
}
}
}
5.新建测试类:
public class TeacherTest {
public static void main(String[] args) {
Teacher teacher=new Teacher("屈仁均");
Course course=new Course("Java课");
Clazz clazz=new Clazz("互联17-1班");
ClassRoom classroom=new ClassRoom("实实南402");
try {
teacher.gotoClass(course,classroom,clazz);
} catch (Exception e) {
throw new RuntimeException(course.getCoursename()+"没老师上,需要代课",e);
}
}
}
附上运行结果: