(仅记录自己的学习之路)
本题目要求编程实现5个类,具体要求如下:
要求:
(1)画出这些类的UML图,UML类图中可以省略成员变量的getXXX()和setXXX()方法。
(2)实现上述各个类,并编写测试类测试这些类的toString()方法。
代码如下:
package TestExtends;
class Person{
private String name;
private String address;
private String phone;
private String email;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Person(String name, String address) {
this.name = name;
this.address = address;
}
public Person(String name, String address, String phone, String email) {
this(name, address);
this.phone = phone;
this.email = email;
}
public String toString() {
return "姓名:" + name + '\n' + "住址:" + address + '\n' + "电话:" + phone + '\n' + "邮箱:" + email + '\n';
}
}
class Student extends Person{
private int grade;
public Student(){
}
public Student(String n, String a, String p, String e, int grade){
super(n, a, p, e);
this.grade = grade;
}
public String toString() {
return super.toString() + "年级:" + grade + '\n';
}
}
class MyDate{
private int year;
private int month;
private 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 year + "年" + month + "月" + day + "日";
}
}
class Employee extends Person{
private String office;
private double salary;
private MyDate date;
public Employee() {
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public MyDate getDate() {
return date;
}
public void setDate(MyDate date) {
this.date = date;
}
public Employee(String n, String a, String p, String e, String office, double salary, MyDate date) {
super(n, a, p, e);
this.office = office;
this.salary = salary;
this.date = date;
}
public String toString() {
return super.toString() + "办公室:" + office + '\n' + "工资:" + salary + '\n' + "受聘日期:" + date.toString() + '\n';
}
}
class Faculty extends Employee{
private String office_hours;
private int level;
public Faculty() {
}
public String getOffice_hours() {
return office_hours;
}
public void setOffice_hours(String office_hours) {
this.office_hours = office_hours;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public Faculty(String n, String a, String p, String e, String o, double s, MyDate d, String office_hours, int level) {
super(n, a, p, e, o, s, d);
this.office_hours = office_hours;
this.level = level;
}
public String toString() {
return super.toString() + "办公时间:" + office_hours + '\n' +"级别:" + level + '\n';
}
}
class Staff extends Employee{
private String title;
public Staff() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Staff(String n, String a, String p, String e, String o, double s, MyDate d, String title) {
super(n, a, p, e, o, s, d);
this.title = title;
}
public String toString() {
return super.toString() + "头衔:" + title + '\n';
}
}
public class TestExtends {
public static void main(String[] args) {
Person p = new Person();
p.setName("赵钱");
p.setAddress("北京市朝阳区");
p.setPhone("12345678901");
p.setEmail("[email protected]");
System.out.println(p.toString());
System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");
Student s = new Student("孙李", "北京市海淀区", "13456789012","[email protected]", 1);
System.out.println(s.toString());
System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");
Employee e = new Employee();
e.setName("周吴");
e.setAddress("北京市西城区");
e.setPhone("14567893201");
e.setEmail("[email protected]");
e.setOffice("网工系办公室");
e.setSalary(7500);
e.setDate(new MyDate(2000,11,8));
System.out.println(e.toString());
System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");
Faculty f = new Faculty("郑王", "北京市东城区", "15678943210", "[email protected]", "计科系办公室", 8000, new MyDate(1999,9,21), "9:00—17:00", 1);
System.out.println(f.toString());
System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");
Staff sf = new Staff("冯陈", "北京市朝阳区", "16789543210", "[email protected]", "软工系办公室", 9000, new MyDate(2000,11,28), "管理员");
System.out.println(sf.toString());
}
}