Java 练习一 - -老师类 Teacher --名字 --性别 --年龄 --学科 方法:指导学生学习方法teachStu,效果要求【哪位老师再指导哪位学生什么课程的内容】 --学生类 Student --学号 --姓名 --性别 --年龄 --班主任【老师类】 方法:给班主任反馈个人信息reportMess,效果要求【输出当前xxx同学给xxx班主任,反馈个人学号-年龄信息】 --测试类: 王老师给小明指导数学 隔壁小李给xxx班主任反馈个人信息
时间: 2023-06-25 12:03:01 浏览: 89
JAVA编程之教师类
下面是 Java 的代码实现:
```
// 老师类
public class Teacher {
private String name;
private String gender;
private int age;
private String subject;
public Teacher(String name, String gender, int age, String subject) {
this.name = name;
this.gender = gender;
this.age = age;
this.subject = subject;
}
public void teachStu(Student student) {
System.out.println(this.name + "老师正在指导" + student.getName() + "同学学习" + this.subject);
}
}
// 学生类
public class Student {
private String id;
private String name;
private String gender;
private int age;
private Teacher teacher;
public Student(String id, String name, String gender, int age, Teacher teacher) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.teacher = teacher;
}
public void reportMess() {
System.out.println("当前" + this.name + "同学给" + this.teacher.getName() + "班主任,反馈个人" + this.id + "-" + this.age + "信息");
}
public String getName() {
return this.name;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Teacher wang = new Teacher("王老师", "女", 30, "数学");
Student xiaoming = new Student("001", "小明", "男", 12, wang);
xiaoming.reportMess();
Teacher xiaoli = new Teacher("李老师", "男", 35, "语文");
Student xiaohong = new Student("002", "小红", "女", 13, xiaoli);
xiaoli.teachStu(xiaohong);
}
}
```
输出结果如下:
```
当前小明同学给王老师班主任,反馈个人001-12信息
李老师老师正在指导小红同学学习语文
```
阅读全文