张大鼻 2016-04-27 22:56 采纳率: 50%
浏览 1626
已采纳

如何用语句去实现多层继承

Person类,有Student类和Employee类继承Person,
而Employee类又被教员类Teacher类和职员类Staff类继承。

每个人都有姓名、地址、电话号码和电子邮件地址。

学生有年级状态(大一到大四)。
雇员有办公室(office)、工资(salary)和受聘日期(dateOfEmployee)。
教员有办公时间(wordTime)和级别(level)。
职员有职位(job)。

覆盖每个类的toString()方法,显示相应的类名和人名。

编写一个测试程序,创建Student、Employee、Teacher和Staff,并且调用它们的toString()方法。

二、在一的基础上改进
1.按名字搜索某个人,找到的打印出来。
2.按雇员的入职日期查找某个时间段内的资料。
3.按名字删除某人

三、在一堆有Student,Teacher,Staff的数据中,删除"大三"的学生

第一步已经敲出来了,第二步怎么用for遍历,跪求大神指教!!!
package jicheng1;

public class ch08 {

public static void main(String[] args) {        
    Person person = new Person("Jack","4区5 220" ,"18059125704","1039572808@qq.com");
    Student student = new Student("Nike", "4区5 221","18059125705", "1039572809@qq.com",Student.one);
    Employee employee = new Employee("Mark","4区5 601","18059125706","1039572810@qq.com","大层楼409","8000","2015.5.20");
    Teacher teacher = new Teacher("Bill","4区5 602","1805912507","1039572811@qq.com","大层楼410", "10000","2015.5.21","早上8点至下午4点","教授");
    Staff staff = new Staff("William","4区5 603","1805912508","1039572812@qq.com","大层楼411", "12000","2015.5.22","教务主任");

    System.out.println("类名:"+person.getClass().getName()+person.toString());
    System.out.println("类名:"+student.getClass().getName()+student.toString());
    System.out.println("类名:"+employee.getClass().getName()+employee.toString());
    System.out.println("类名:"+teacher.getClass().getName()+teacher.toString());
    System.out.println("类名:"+staff.getClass().getName()+staff.toString());     
}

}
class Person{
protected String name,address,telNum,email;
public Person(String name,String address,String telNum,String email){
this.name = name;
this.address = address;
this.telNum =telNum;
this.email = email;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public String getTelNum(){
return telNum;
}
public String getEmail(){
return email;
}
public String toString(){
return "人名"+this.getName();
}
}
class Student extends Person{
protected int grade;
final static int one = 1,two = 2,three = 3,four = 4 ;

public Student(String name,String address,String telNum,String email,int grade){
super(name,address,telNum,email);
this.grade = grade;

}
public int findGrade(int grade){
return grade;

}
public int getGrade(){
    return grade;
}
public String toString(){
    return "人名"+this.getName();
}

}
class Employee extends Person{
protected String office,salary,dataOfEmployee;
public Employee(String name,String address,String telNum,String email,String office,String salary,String dataOfEmployee){
super(name,address,telNum,email);
this.office = office;
this.salary = salary;
this.dataOfEmployee = dataOfEmployee;
}
public String getOffice(){
return office;
}
public String getSalary(){
return salary;
}
public String getDataOfEmployee(){
return dataOfEmployee;
}
public String toString(){
return "人名"+this.getName();
}
}
class Staff extends Employee{
protected String job;
public Staff(String name,String address,String telNum,String email,String office,String salary,String dataOfEmployee,String job){
super(name,address,telNum,email,office,salary,dataOfEmployee);
this.job = job;
}
public String getJob(){
return job;
}
public String toString(){
return "人名"+this.getName();
}
}
class Teacher extends Employee{
protected String wordTime,level;
public Teacher(String name,String address,String telNum,String email,String office,String salary,String dataOfEmployee,String wordTime,String level){
super(name, address, telNum, email, office, salary, dataOfEmployee);
this.wordTime = wordTime;
this.level = level;
}
public String getWordTime(){
return wordTime;
}
public String getLevel(){
return level;
}
public String toString(){
return "人名"+this.getName();
}
}

  • 写回答

6条回答

  • 破小孩儿 2016-04-28 01:20
    关注

    LZ,你这是把课堂作业带到CSDN里来找答案了么?
    不直接告诉你答案,指点你一下:用for + instanceof就能解决第二步的问题。
    “3.按名字删除某人”,建议用迭代器来遍历哦~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题