定义一个部门类Department
属性:部门编号、部门名称、部门人数、部门职责、部门经理
定义两个构造方法:无参和有参各一
定义成员方法:输出信息printDetai10
其有一个子类研发部类ResearchDept
屈性:研发方向speciality
在构造方法中调用父类构造方法
重写printDetail0方法
在测试类中测试
该回答引用自ChatGPT
public class Department {
private String deptNo;
private String deptName;
private int numOfEmployees;
private String deptResponsibility;
private String deptManager;
public Department() {
}
public Department(String deptNo, String deptName, int numOfEmployees, String deptResponsibility, String deptManager) {
this.deptNo = deptNo;
this.deptName = deptName;
this.numOfEmployees = numOfEmployees;
this.deptResponsibility = deptResponsibility;
this.deptManager = deptManager;
}
public void printDetail() {
System.out.println("Department No.: " + deptNo);
System.out.println("Department Name: " + deptName);
System.out.println("Number of Employees: " + numOfEmployees);
System.out.println("Department Responsibility: " + deptResponsibility);
System.out.println("Department Manager: " + deptManager);
}
}
public class ResearchDept extends Department {
private String speciality;
public ResearchDept(String deptNo, String deptName, int numOfEmployees, String deptResponsibility, String deptManager, String speciality) {
super(deptNo, deptName, numOfEmployees, deptResponsibility, deptManager);
this.speciality = speciality;
}
@Override
public void printDetail() {
super.printDetail();
System.out.println("Speciality: " + speciality);
}
}
public class TestDepartment {
public static void main(String[] args) {
Department dept1 = new Department("001", "Sales", 50, "Selling products and services", "John Doe");
dept1.printDetail();
System.out.println();
ResearchDept dept2 = new ResearchDept("002", "Research and Development", 20, "Researching new technologies", "Jane Smith", "Artificial Intelligence");
dept2.printDetail();
}
}
输出结果:
Department No.: 001
Department Name: Sales
Number of Employees: 50
Department Responsibility: Selling products and services
Department Manager: John Doe
Department No.: 002
Department Name: Research and Development
Number of Employees: 20
Department Responsibility: Researching new technologies
Department Manager: Jane Smith
Speciality: Artificial Intelligence
该回答引用ChatGPT
代码中,定义了Department类,其中包括了属性:部门编号、部门名称、部门人数、部门职责、部门经理。同时也定义了两个构造方法,无参构造方法和有参构造方法,用于初始化部门对象。还定义了一个成员方法printDetail,用于输出部门信息。
在ResearchDept类中,除了继承了Department类的属性和方法之外,还添加了研发方向属性speciality。在构造方法中,调用了父类Department的有参构造方法,初始化了ResearchDept类的属性。并且,还重写了printDetail方法,在输出Department类的属性之后,还输出了研发部门特有的属性研发方向。
最后,在测试类TestDepartment中,创建了一个Department对象和一个ResearchDept对象,分别调用它们的printDetail方法,输出它们的属性。
代码如下
public class Department {
private String deptId;
private String deptName;
private int numOfEmployees;
private String duties;
private String manager;
public Department() {}
public Department(String deptId, String deptName, int numOfEmployees, String duties, String manager) {
this.deptId = deptId;
this.deptName = deptName;
this.numOfEmployees = numOfEmployees;
this.duties = duties;
this.manager = manager;
}
public void printDetail() {
System.out.println("Department ID: " + deptId);
System.out.println("Department Name: " + deptName);
System.out.println("Number of Employees: " + numOfEmployees);
System.out.println("Department Duties: " + duties);
System.out.println("Department Manager: " + manager);
}
}
public class ResearchDept extends Department {
private String speciality;
public ResearchDept(String deptId, String deptName, int numOfEmployees, String duties, String manager, String speciality) {
super(deptId, deptName, numOfEmployees, duties, manager);
this.speciality = speciality;
}
@Override
public void printDetail() {
super.printDetail();
System.out.println("Research Department Speciality: " + speciality);
}
}
public class TestDepartment {
public static void main(String[] args) {
Department dept1 = new Department("001", "Human Resources", 10, "Employee management and recruitment", "John Smith");
dept1.printDetail();
ResearchDept researchDept = new ResearchDept("002", "Research and Development", 20, "Research and development of new products", "Jane Doe", "AI and Machine Learning");
researchDept.printDetail();
}
}