问题描述:定义一个类:团队(Team),该类中包含一些其它类的对象(教师,研究生,本科生等),为该类定义一个有参的构造方法及一些方法来描述团队的行为。
代码实现如下:
Team类(团队,团队中包括教师,研究生,本科生):
package experiment2;
public class Team {
private String name;//团队名称
private int max;//团队人数
private String pName;//项目名称
private Teacher teacher;//指导老师
private GraduateStudent[] gStudents;//研究生数组
private UnderGStudent[] uGStudents; //本科生数组
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setMax(int max) {
this.max = max;
}
public int getMax() {
return max;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public GraduateStudent[] getgStudents() {
return gStudents;
}
public void setgStudents(GraduateStudent[] gStudents) {
this.gStudents = gStudents;
}
public UnderGStudent[] getuGStudents() {
return uGStudents;
}
public void setuGStudenst(UnderGStudent[] uGStudents) {
this.uGStudents = uGStudents;
}
public Team(String name,int max,String pName) {
this.setpName(pName);
this.setName(name);
this.setMax(max);
gStudents = new GraduateStudent[max/2];
uGStudents = new UnderGStudent[max-max/2];
}
//指导老师加入团队
public void tInTeam(Teacher tTeacher){
teacher = tTeacher;
System.out.println("指导老师" + teacher.getName() + "成功加入" + getName()+"团队" );
}
//研究生加入团队
public void gSInTeam(GraduateStudent gStudent){
for(int i=0;i<gStudents.length;i++) {
if(gStudents[i]==null) {
gStudents[i]=gStudent;
System.out.println("研究生" + gStudent.getName() + "成功加入" + getName()+"团队" );
break;
}
if(i==gStudents.length-1) {
System.out.println( "研究生" + gStudent.getName() + "加入失败," +getName()+"团队研究生组已满员");
}
}
}
//本科生加入团队
public void uGSInTeam(UnderGStudent uGStudent) {
for(int i=0;i<uGStudents.length;i++) {
if(uGStudents[i]==null) {
uGStudents[i]=uGStudent;
System.out.println("本科生" + uGStudent.getName() + "成功加入" + getName()+"团队" );
break;
}
if(i==uGStudents.length-1) {
System.out.println( "本科生" + uGStudent.getName()+"加入失败," + getName()+"团队本科生组已满员");
}
}
}
//输出团队信息
public void outputInfo() {
System.out.println("团队名称:" + getName() + " 指导老师:" + getTeacher().getName() + " 项目名称:" + getpName());
System.out.println("研究生成员:");
for(int i = 0;i < gStudents.length;i++){
if(gStudents[i] != null)
System.out.printf(gStudents[i].getName()+" ");
}
System.out.println();
System.out.println("本科生成员:");
for(int i = 0;i < uGStudents.length;i++)
{
if(uGStudents[i] != null)
System.out.printf(uGStudents[i].getName()+" ");
}
System.out.println();
}
//指导老师指导项目
public void guideProject() {
System.out.println("指导老师" + getTeacher().getName() + "正在指导" + getpName()+ "团队项目。");
}
}
Teacher类(指导老师):
package experiment2;
public class Teacher {
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Teacher(String name,String sex,int age) {
this.setName(name);
this.setSex(sex);
this.setAge(age);
}
}
GraduateStudent类(研究生):
package experiment2;
public class GraduateStudent {
private String name;
private String sex;
private int age;
private String eduBack;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEduBack() {
return eduBack;
}
public void setEduBack(String eduBack) {
this.eduBack = eduBack;
}
public GraduateStudent(String name,String sex,int age,String eduBack) {
this.setName(name);
this.setSex(sex);
this.setAge(age);
this.setEduBack(eduBack);
}
}
UnderGStudent类(本科生):
package experiment2;
public class UnderGStudent {
private String name;
private String sex;
private int age;
private String eduBack;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEduBack() {
return eduBack;
}
public void setEduBack(String eduBack) {
this.eduBack = eduBack;
}
public UnderGStudent(String name,String sex,int age,String eduBack) {
this.setName(name);
this.setSex(sex);
this.setAge(age);
this.setEduBack(eduBack);
}
}
TeamTest类(测试类):
package experiment2;
public class TeamTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Teacher teacher = new Teacher("杨斌","男",30);
GraduateStudent gStudent1 = new GraduateStudent("李超","男",23,"研究生");
GraduateStudent gStudent2 = new GraduateStudent("刘丽","女",22,"研究生");
UnderGStudent uGStudent1 = new UnderGStudent("张三","男",21,"本科生");
UnderGStudent uGStudent2 = new UnderGStudent("李颖","女",20,"本科生");
UnderGStudent uGStudent3 = new UnderGStudent("姚磊","男",20,"本科生");
UnderGStudent uGStudent4 = new UnderGStudent("刘芳","女",21,"本科生");
Team team = new Team("题都做不队",5,"不知道是什么");
team.tInTeam(teacher);
team.gSInTeam(gStudent1);
team.gSInTeam(gStudent2);
team.uGSInTeam(uGStudent1);
team.uGSInTeam(uGStudent2);
team.uGSInTeam(uGStudent3);
team.uGSInTeam(uGStudent4);
team.outputInfo();
team.guideProject();
}
}
查看输出结果:
到此,符合题目条件的一个简单的 类就写好了。