2 qq 34776487 qq_34776487 于 2016.04.28 06:56 提问

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

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个回答

zjysource
zjysource   2016.04.28 09:20
已采纳

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

qq_34776487
qq_34776487 请教一下,第三步也是用迭代器吗
一年多之前 回复
qq_34776487
qq_34776487 谢谢 ,这个知识点不是很熟,但我会去试试.
一年多之前 回复
devmiao
devmiao   Ds   Rxr 2016.04.28 06:59
 package com.xujin;

public class Test {
    public static void main(String[] args) {
        Employee[] staff = new Employee[3];
        staff[0] = new Employee("Bob", 1000);
        staff[1] = new Manager("Jim", 5000, 1000);
        staff[2] = new Boss("Alice", 7000, 1000, 10000);

        for(Employee e : staff)
            System.out.println("class name:" + e.getClass().getName() + "\tid:" + e.getId() + 
                    "\tname:" + e.getName() + "\tsalary:" + e.getSalary());

        Manager man = (Manager)staff[1];
        Boss boss = (Boss)staff[2];
        System.out.println(man.getBonus());//类型转换后就可以使用实际类型的全部功能
        System.out.println(boss.getAward());

        //ClassCastException异常,不允许进行继承链上的从上到下的转换
        //Boss myBoss = (Boss)staff[0];

        //把instaceof运算符和类型转换组合起来,避免异常
        if(staff[0] instanceof Boss){
            System.out.println("staff[0] is a instace of Boss");
            Boss myBoss = (Boss) staff[0];
        }
        else System.out.println("staff[0] isn't a instace of Boss");

        if(staff[2] instanceof Boss){
            System.out.println("staff[2] is a instace of Boss");
        }
        else System.out.println("staff[2] isn't a instace of Boss");

    }   
}

class Employee{     
    public Employee(String name){
        this.name = name;
        id = nextId;
        nextId++;
    }

    public Employee(String name, double salary){
        this(name);//调用另一构造器      
        this.salary = salary;       
    }

    //定义访问器方法
    public final String getName(){
        return name;
    }

    public double getSalary(){
        return salary;
    }

    public final int getId(){
        return id;
    }


    //定义更改器方法
    public final void setName(String name){
        this.name = name;
    }

    public final void setSalary(double salary){
        this.salary = salary;
    }   

    public final void raiseSalary(double percent){
        this.salary *= (1 + percent);
    }

    //定义变量
    private String name = "";//实例域初始化
    private double salary;
    private int id;
    private static int nextId = 1;      

}

class Manager extends Employee{
    public Manager(String name, double salary, double bonus){
        super(name, salary);//super在构造器中的使用,可以调用超类的构造器
        setBonus(bonus);
    }   

    public double getBonus(){
        return bonus;
    }

    //重写getSalary方法
    public double getSalary(){
        double baseSalary = super.getSalary();//调用了超类的getSalary方法
        return baseSalary + bonus;
    }

    public void setBonus(double bonus){
        this.bonus = bonus;
    }

    private double bonus;
}

final class Boss extends Manager{
    public Boss(String name, double salary, double bonus, double award){
        super(name, salary, bonus);
        this.award = award;
    }

    //重写getSalary方法
    public double getSalary(){
        double baseSalary = super.getSalary();//调用了超类的getSalary方法
        return baseSalary + award;
    }

    public double getAward(){
        return award;
    }
    private double award;
}
qq_34776487
qq_34776487 谢谢你.
一年多之前 回复
wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.04.28 08:50

java语言吧,java没有多继承这个概念呢。

qq_25827845
qq_25827845   2016.04.28 09:03

不懂围观........话说要实现二里边的要求,不应该是把数据存入数据库,然后从数据库里查找,删除么?

sl_18500
sl_18500   2016.04.28 10:56

深入理解继承的概念吧

donggua3694857
donggua3694857   Rxr 2016.04.28 11:13

lz,java里面没有多继承这种说法。按名字搜索某人,这些数据是不是都在数据库里啊,如果是就是数据库查询

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!