继承与多态的一个简单实例

程序结构:
父为Person类,Person类个子类Student和Employee,Employee类又有两个子类,Staff和Faculty。

继承即子类可以继承父类的数据域和方法:

Student类和Employee类继承了Person类的数据域name,address phoneNumber E_mail className 以及他们的set方法和get方法。同时Emloyee类中声明了新的数据域office,wage,date以及他们的set get方法。Student类有新的grade数据域和方法。
Staff和Faculty类继承了Employee类中的所有方法,包括从Person类中继承来的方法,同时有定义了自己的新方法。
同时,子类通过super调用了父类设置日期的构造方法和toString方法。

多态:
TestPerson类中创建了Student,Staff和Faculty类型的对象
在调用toString时,每个类都有自己对toString方法的实现(子类覆盖了父类的toString方法),最终的输出结果取决于对象的类型,体现了动态绑定。

继承与多态的一个简单实例_第1张图片


package person;
//父类
public class Person {
	protected String name ;                       //姓名
	protected String address;                      //地址
	protected String phoneNumber;                  //电话号码
	protected String E_mail;                       //电子邮件
	protected String className;                    //类名,即学生、职员、教员
	public void setE_mail() {               
		this.E_mail=E_mail;
	}
	public String getE_mail() {
		return E_mail;
	}
	public void setPhoneNumber() {
		this.phoneNumber=phoneNumber;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
    public void setClassName() {
    	this.className=className;
    }
	public String getClassName() {
		return className;
	}
	public void setAddress() {
		this.address=address;
	}
	public String getAddress() {
		return address;
	}
	
	public void setName() {
		this.name=name;
	}
	public String getName() {
		return name;
	}
	public String toString() {                        
		return getClassName()+"  \n"+getName()+"  \nphnone number is:\n"+getPhoneNumber()+" \nE-mail is :\n"+getE_mail()+" \naddress is :\n"+getAddress();
	}
	
	
	

}

package person;
public class Student extends  Person{
	protected String grade;
	final static int  RRESHMAN=1;
	final static int SOPHOMORE=2;
	final static int JUNIOR=3;
	final static int SENIOR=4;
	public void setGrade() {
		this.grade=grade;
	}
	public String getGrade() {
		return grade;
	}
	public Student(String className,String name,String grade,String phoneNumber,String E_mail,String address) {
		this.className=className;
		this.name=name;
		this.grade=grade;
		this.phoneNumber=phoneNumber;
		this.E_mail=E_mail;
		this.address=address;
	}
    public String toString() {
    	return super.toString()+"\ngrade is :\n"+grade;
    }

}


package person;


public class Employee extends Person {
	protected String office;                        //办公室
	protected double wage;                           //工资
    MyDate date;                                    //MyDate的一个对象,作用是在employee里面创建一个构造方法,使在子类中能调用他的构造方法
	
    public Employee(int year,int month,int day) {
    	 this.date=new MyDate(year,month,day);
    
    }
    public void setOffice() {
    	this.office=office;
    }
    public String getOffice() {
    	return office;
    }
    public void setWage() {
    	this.wage=wage;
    }
    public double getWage() {
    	return wage;
    }
    
 
    
    public String toString() {
    	return super.toString()+"\noffice is :\n"+getOffice()+"\nwage is :\n"+getWage()+"\nthe year is :\n"+MyDate.getYear()+
    			"\nthe month is:\n"+MyDate.getMonth()+"\nthe day is:\n"+MyDate.getDay();
    }
    
   

}


package person;
//教员
public class Faculty extends Employee {

	protected String rank;                //等级
	protected String time;                //工作时间如9:00-13:00

	public void setRank() {
		this.rank=rank;
	}
	public String getRank() {
		return rank;
	}
	public void setTime() {
		this.time=time;
	}
	public String getTime() {
		return time;
	}
	public Faculty(String className,String name,String rank,String phoneNumber,String E_mail,String address,String office,double wage,String time,int year,int month,int day){
		    super(year,month,day);
		    this.className=className;
	    	this.name=name;	    
	    	this.rank=rank;
	    	this.phoneNumber=phoneNumber;
	    	this.E_mail=E_mail;
	    	this.address=address;
	    	this.office=office;
	    	this.wage=wage;
	    	this.time=time;
	}
	
	
    public String toString() {
    	return super.toString()+"\nrank is :\n"+rank+"\nthe work time is:\n"+time;
    }
	


}


package person;

//职员

public class Staff extends Employee {

	protected String staffTitle;                //职位
	
	public void setStaffTitle() {
		this.staffTitle=staffTitle;
	}
	public String getStaffTitle() {
		return staffTitle;
	}
	public Staff(String className,String name,String staffTitle,String phoneNumber,String E_mail,String address,String office,double wage,int year,int month,int day) {
		super(year,month,day);
		this.className=className;
		this.name=name;
		this.staffTitle=staffTitle;
		this.phoneNumber=phoneNumber;
		this.E_mail=E_mail;
		this.address=address;
    	this.name=name;

    	this.office=office;
    	this.wage=wage;
    	
	}
	public Staff() {                   //无参的构造方法,默认方法
		this("staff","lisi","cleaner","1527319","[email protected]","北京市","机械院203",3000,2015,3,12);
	}
    public String toString() {
    	return super.toString()+"\nstaffTitle is :\n"+staffTitle;
    }

}


package person;

public class MyDate {

	    	public static int  year;
	    	public static int  month;
	    	public static int day;
	    	public MyDate(int year,int month,int day) {
	    		this.year=year;
	    		this.month=month;
	    		this.day=day;
	    	}
	    	public void setYear() {
	    		this.year=year;
	    	}
	    	public static int getYear() {
	    		return year;
	    	}
	    	public void setMonth() {
	    		this.month=month;
	    	}
	    	public static int getMonth() {
	    		return month;
	    	}
	    	public void setDay() {
	    		this.day=day;
	    	}
	    	public static int getDay() {
	    		return day;
	    	}
}

package person;


public class TestPerson {
	public static void main(String[] args) {
		m(new Student("student","zengmeng","junior","12345","[email protected]","四川成都"));           //初始化学生对象
		m(new Faculty("Faculty","zhangsan","associate professor","139822","[email protected]","湖南长沙","环境院408",10000,"9:00-18:00",2018,6,20));
		m(new Staff());         //初始化职员对象,无参构造
	
	}
	public static void m(Object x) {
		System.out.println(x.toString());		  //输出
	}
}



运行结果

继承与多态的一个简单实例_第2张图片

你可能感兴趣的:(Java基础学习)