【JAVA语言程序设计第十版 11.2】+ 多态 + 继承

来源:互联网 发布:feplayer.js制作弹幕 编辑:程序博客网 时间:2024/06/05 06:55

Person类代码:

package Less1;public class Person {    private String  name, add, tel, ema;    public Person(){}    public Person(String names,String add, String tel, String ema){        this.name = names;        this.add = add;        this.tel = tel;        this.ema = ema;    };    public String toString(){        return "Person" +" "+ name + add + tel + ema;    }}

Student类代码:

public class Student  extends Person{    private int grade;    public Student(){}    int bj;    public Student(String names,String add, String tel, String ema,int bj){        super (names,add,tel,ema);        this.bj = bj;    };    public String toString(){        return "Student"+" "+super.toString() + bj;    }}

Employee类代码:

public class Employee extends Person{    private String office;    private int wages;    private java.util.Date date;    public Employee(){         date = new java.util.Date();    }    String room,money,dat;    public Employee(String names,String add, String tel, String ema,String room,String money,String dat){        super(names,add,tel,ema);        date = new java.util.Date();        this.room = room;        this.money = money;        this.dat = dat;    };    public String toString(){        return "Employee"+" "+ super.toString() + room + money + dat;    }}

Faculty类代码:

public class Faculty extends Employee{    private String  wtime; int gra;    public Faculty(){}    String time,jb;    public Faculty(String names,String add, String tel, String ema,String room,String money,String dat,String time,String jb){        super(names,add,tel,ema,room,money,dat);        this.time = time;        this.jb = jb;    };    public String toString(){        return "Faculty"+" "+super.toString() + time + jb;    }}

staff类代码:

public class Staff extends Employee{    private String wname;    public Staff(){};    String zh;    public Staff(String names,String add, String tel, String ema,String room,String money,String dat,String zh){        super(names,add,tel,ema,room,money,dat);        this.zh = zh;    };    public String toString(){        return "Staff"+" " + super.toString() + zh;    }}

Test代码:

public class Main {    public static void main(String[] args) {        // TODO Auto-generated method stub        Object stu = new Student("xiaoming","Henan" , "188" , "182" ,15);        Object fac = new Faculty("xiaohua","Henan" , "188" , "182","110","666" , "1", "1","1");        Object sta = new Staff("xiaozhang","Henan", "188", "182","101","1010","1100" , "1");        System.out.println(stu.toString());        System.out.println(fac.toString());        System.out.println(sta.toString());    }}
0 0
原创粉丝点击