Welcome to JAVA!之Person类

来源:互联网 发布:c语言数组结束符 编辑:程序博客网 时间:2024/06/05 22:58
11.2.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,phone number,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.Define a class named MyDate that contains the fields year,month,and day,A faculty member has office hours and a rank,A staff member has a title,Override the toString method in each class to display the class name and the person's name.

代码如下:

Person.java

public class Person {private String name;private String address;private String phone;private String email;public Person(){}public Person(String name,String address,String phone,String email){this.name=name;this.address=address;this.phone=phone;this.email=email;}public void setName(String name){this.name=name;}public String getName(){return name;}public void setAddress(String address){this.address=address;}public String getAddress(){return address;}public void setPhone(String phone){this.phone=phone;}public String getPhone(){return phone;}public void setEmail(String email){this.email=email;}public String getEmail(){return email;}public String toString(){return "A Person\n"; }}
Employee.java

public class Employee extends Person {private int office;private int salary;MyDate dateHired; public Employee() {}public Employee(int office,int salary,int year,int month,int day){ this.office=office; this.salary=salary; dateHired = new MyDate(year,month,day);} public Employee(String name, String address, String phone, String email,int office,int salary,int year,int month,int day) { super(name, address, phone, email); this.office=office; this.salary=salary; dateHired = new MyDate(year,month,day); } public void setOffice(int office){this.office=office;}public int getOffice(){return office;}public void setSalary(int salary){this.salary=salary;}public int getSalary(){return salary;}class MyDate {public int year;public int month;public int day;public MyDate(){}public MyDate(int year,int month,int day){ this.year=year; this.month=month; this.day=day;}}    public String toString()      {          return "This is a Employee,office is " + office +"and salary is " + salary + ",Entry time is "+ dateHired.year +"-"+dateHired.month + "-" +dateHired.day;              }  }
Student.java

public class Student extends Person {private int num=0;private String status[]={"freshman","sophomore","junior","senior"};public Student() {}public Student(int num) {this.num=num-1;}public Student(String name, String address, String phone, String email,int num) {super(name, address, phone, email);this.num=num-1;}public void setStatus(int num) {this.num=num-1;}public String getStatus(){return status[num];}public String toString(){return "This is a Student.status is " +status[num];}}
Faculty.java

public class Faculty extends Employee {private int officehours;private int rank;public Faculty(){}public Faculty(String name, String address, String phone, String email,int office,int salary,int year,int month,int day,int officehours,int rank){super(name,address,phone,email,office,salary,year,month,day);this.officehours=officehours;this.rank=rank;}public void setOfficeHours(int officehours){this.officehours=officehours;}public int getOfficeHours(){return officehours;}public void setRank(int rank){this.rank=rank;}public int getRank(){return rank;}public String toString()      {          return "This is a Faculty.officehours is " + officehours + " and rank is " + rank;      }  }
Staff,java

public class Staff extends Employee{private String title;public Staff() {}public Staff(String name, String address, String phone, String email,int office,int salary,int year,int month,int day,String title) {super(name,address,phone,email,office,salary,year,month,day);this.title=title;}public void setTitle(String title){this.title=title;}public String getTitle(){return title;}public String toString()      {          return "This is a Staff.title is " + title;      }  }
TestPerson.java

public class TestPerson {public static void main(String[] args) {Student student = new Student("王二","山东","12345678910","wang@163.com",2);System.out.println("The student's name:" + student.getName());System.out.println("The student's address:"+ student.getAddress());System.out.println("The student's phone: " + student.getPhone());System.out.println("The student's email: " + student.getEmail());System.out.println(student.toString());Employee employee = new Employee("李四","山东","12345679800","Lisi@163.com",5,10000,2005,12,1);System.out.println("\nThe employee's name:" + employee.getName());System.out.println("The employee's address:"+ employee.getAddress());System.out.println("The employee's phone: " + employee.getPhone());System.out.println("The employee's email: " + employee.getEmail());System.out.println(employee.toString());Faculty faculty = new Faculty("杨六","四川","12345678999","yangliu@163.com",3,12000,2004,6,30,8,10);System.out.println("\nThe faculty's name:" + faculty.getName());System.out.println("The faculty's address:"+ faculty.getAddress());System.out.println("The faculty's phone: " + faculty.getPhone());System.out.println("The faculty's email: " + faculty.getEmail());System.out.println(faculty.toString());Staff staff = new Staff("辛八","江西","12345678889","xinba@163.com",2,13000,2000,5,9,"妹控");System.out.println("\nThe staff's name:" + staff.getName());System.out.println("The staff's address:"+ staff.getAddress());System.out.println("The staff's phone: " + staff.getPhone());System.out.println("The staff's email: " + staff.getEmail());System.out.println(staff.toString());}}

运行结果:

/*output:The student's name:王二The student's address:山东The student's phone: 12345678910The student's email: wang@163.comThis is a Student.status is sophomoreThe employee's name:李四The employee's address:山东The employee's phone: 12345679800The employee's email: Lisi@163.comThis is a Employee,office is 5and salary is 10000,Entry time is 2005-12-1The faculty's name:杨六The faculty's address:四川The faculty's phone: 12345678999The faculty's email: yangliu@163.comThis is a Faculty.officehours is 8 and rank is 10The staff's name:辛八The staff's address:江西The staff's phone: 12345678889The staff's email: xinba@163.comThis is a Staff.title is 妹控*///~










1 0
原创粉丝点击