实验目的:
掌握对象的组合及参数传递
Teacher.java
public class Teacher{
int teacherType;
public void speak(){
if(teacherType==1){
System.out.println("课程的内容是二次方程");
}
else if(teacherType==2){
System.out.println("课程的内容是学唱五线谱");
}
}
}
School.java
public class School{
Teacher mathTeacher,musicTeacher;
void setTeacher(Teacher t1,Teacher t2){
mathTeacher=t1;
musicTeacher=t2;
}
void startMathLesson() {
mathTeacher.speak();
}
void startMusicLesson(){
musicTeacher.speak();
}
}
MainClass.java
public class MainClass {
public static void main(String[] args) {
Teacher zhang,wang;
zhang=new Teacher();
wang=new Teacher();
zhang.teacherType=1;
wang.teacherType=2;
School 实验中学=new School();
实验中学.setTeacher(zhang,wang);
实验中学.startMathLesson();
实验中学.startMusicLesson();
}
}
运行结果: