Write a test program that creates a Person , Student , Employee , Faculty , and Staff , and invok

    Design a class named  Person and its two subclasses named  Student and Employee .Make  Faculty and  Staff subclasses of  Employee . A person has a name,address, phonenumber, and email address. A student has a class status (freshman,sophomore,junior, or senior). Define the status as a constant. An employee has an office,salary, and date hired. A faculty member has office hours and a rank. A staffmember has a title. Override the toString method in each class to display the class name and the person’s name.

    Write a test program that creates a Person , Student , Employee , Faculty , and  Staff , and invokes their  toString() methods.

public class Person {
	String name;  
	String address;  
	String telphone;  
	public Person(String n,String a,String t)
	{   
		name=n;   
		address=a;   
		telphone=t;  
	}  
	public String toString()
	{   
		return name+" Person";    
	}
	public void display(Person person)
	{
		System.out.println(person);
	}
}

public class Employee extends Person{
	String office;
	double salary;
	public Employee(String n,String a,String t,String o,double s)
	{
		super(n, a, t);
		office = o;
		salary = s;
	}
	public String toString()
	{
		return name+"employee";
	}
}

public class Student extends Person{
	final String class1="一年级";
	final String class2="二年级";
	final String class3="三年级";
    final String class4="四年级";
    public Student(String n,String a,String t)
    { 
    	super(n, a, t);
    } 
    public String toString()
    { 
    	return name+" Student";
    }
}

public class Faculty extends Employee{
	int Level;
	public Faculty (String n,String a,String t,String o,double w,int level) 
	{
		super(n, a, t, o, w);
		Level = Level;
	}
	public String toString()
	{
		return name+"Faculty";
	}
}


public class Staff extends Employee{
	String position;
	public Staff(String n,String a,String t,String o,double w,String p)
	{
		super(n,a,t,o,w);
		position=p;
	}
	public String toString()
	{
		return name+"staff";
	}	
}

public class Test {
	public static void main(String[] args) {	
	Person p = new Person("网", "数据库的","2345678" );
	p.display(p);
	Student s = new Student("王宏","河南省漯河市","15839652309");
	s.display(s);
	Employee e = new Employee ("李四","河南省漯河市","0395112222","人事局",222.00);
	e.display(e);
	Faculty f = new Faculty("明明", "河北", "11232312", "帮手", 234,1);
	f.display(f);
	Staff sta = new Staff("红红","河南省周口市","13849472334","人事科",345.00,"副局长");
	sta.display(sta);
	}
}




以下是用C++实现的代码: ```c++ #include <iostream> #include <string> using namespace std; class MyDate { private: int year, month, day; public: MyDate(int y, int m, int d) { year = y; month = m; day = d; } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } }; class Person { private: string name, address, phone, email; public: Person(string n, string a, string p, string e) { name = n; address = a; phone = p; email = e; } virtual string toString() const { return "Person: " + name; } }; class Student : public Person { private: string classStatus; public: Student(string n, string a, string p, string e, string c) : Person(n, a, p, e) { classStatus = c; } virtual string toString() const { return "Student: " + Person::toString(); } }; class Employee : public Person { private: string office; double salary; MyDate dateHired; public: Employee(string n, string a, string p, string e, string o, double s, MyDate d) : Person(n, a, p, e) { office = o; salary = s; dateHired = d; } virtual string toString() const { return "Employee: " + Person::toString(); } }; class Faculty : public Employee { private: string officeHours; string rank; public: Faculty(string n, string a, string p, string e, string o, double s, MyDate d, string oh, string r) : Employee(n, a, p, e, o, s, d) { officeHours = oh; rank = r; } virtual string toString() const { return "Faculty: " + Person::toString(); } }; class Staff : public Employee { private: string title; public: Staff(string n, string a, string p, string e, string o, double s, MyDate d, string t) : Employee(n, a, p, e, o, s, d) { title = t; } virtual string toString() const { return "Staff: " + Person::toString(); } }; int main() { MyDate hireDate(2020, 1, 1); Person person("John Smith", "123 Main St.", "555-1234", "jsmith@example.com"); Student student("Jane Doe", "456 Elm St.", "555-5678", "jdoe@example.com", "sophomore"); Employee employee("Bob Johnson", "789 Oak St.", "555-9012", "bjohnson@example.com", "101", 50000, hireDate); Faculty faculty("Alice Brown", "321 Pine St.", "555-3456", "abrown@example.com", "102", 75000, hireDate, "MWF 10-12", "Professor"); Staff staff("Tom Davis", "654 Cedar St.", "555-7890", "tdavis@example.com", "103", 40000, hireDate, "Administrative Assistant"); cout << person.toString() << endl; cout << student.toString() << endl; cout << employee.toString() << endl; cout << faculty.toString() << endl; cout << staff.toString() << endl; return 0; } ``` 输出结果为: ``` Person: John Smith Student: Person: Jane Doe Employee: Person: Bob Johnson Faculty: Person: Alice Brown Staff: Person: Tom Davis ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值