您的位置:首页 > 编程语言 > Java开发

Welcome to JAVA!之Person类

2015-11-05 18:45 537 查看
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: 12345678910
The student's email: wang@163.com
This is a Student.status is sophomore

The employee's name:李四
The employee's address:山东
The employee's phone: 12345679800
The employee's email: Lisi@163.com
This is a Employee,office is 5and salary is 10000,Entry time is 2005-12-1

The faculty's name:杨六
The faculty's address:四川
The faculty's phone: 12345678999
The faculty's email: yangliu@163.com
This is a Faculty.officehours is 8 and rank is 10

The staff's name:辛八
The staff's address:江西
The staff's phone: 12345678889
The staff's email: xinba@163.com
This is a Staff.title is 妹控
*///~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息