用继承的,已经实现了,没有什么参考答案,所以代码也没有改进。
这里不写继承的,那个似乎比较简单。就是 people,teacher,student,headTeacher一溜的继承,然后创建一个school,里面有老师,学生,班主任,然后科目什么的。
用组合关系,有点没理清楚要怎么具体的去写。。。大概写了个框架
package chapter5.test; class People{ private String name; public void introduction(){ System.out.println("name:"+name); } public People(String name){ this.name=name; } } class HeadTeacher extends People{ private String subject; public String getSubject(){ return subject; } public HeadTeacher(String name,String subject){ super(name); this.subject=subject; } public void teaching(){ System.out.println("teaching "+getSubject()); } public void introduction(){ super.introduction(); System.out.println("subject:"+getSubject()); } } class School{ //学校由 老师,学生,学科等组成 //学校需要调用老师的teaching方法,学生的study方法等等 private String name; HeadTeacher ht; public School(String name,HeadTeacher ht){ this.name=name; this.ht=ht; } public void introduction(){ System.out.println("学校名称:"+name); } public void teaching(){ ht.teaching(); } } public class TestConbination { public static void main(String[]args){ People p =new People("张三"); HeadTeacher ht =new HeadTeacher("李四","物理"); ht.introduction(); ht.teaching(); p.introduction(); System.out.println("---------"); School sch=new School("张家口学校",ht); sch.introduction(); sch.teaching(); } }