按步骤编写代码,效果如图所示:
销售部的:张小强,员工编号:M001
他的职员是李小亮
销售部的:李小亮,员工编号:C001
他的经理是张小强
* 编写步骤:
1. 定义员工Employee类。包含属性:姓名,工号,部门。
2. 定义经理Manager类继承Employee类,包含属性:职员(该经理的职员)。
3. 定义职员Clerk类继承Employee类,包含属性:经理(该职员的经理)。
* 参考答案:
public class Test6 {
public static void main(String[] args) {
// 创建 经理对象
Manager manager = new Manager("M001" , "张小强" , "销售部");
// 创建 职员对象
Clerk clerk = new Clerk("C001" , "李小亮" , "销售部");
// 设置职员信息
manager.setC(clerk);
// 设置经理信息
clerk.setM(manager);
// 展示信息
manager.show();
clerk.show();
}
}
class Employee {
// a)成员变量: 工号(id),姓名(name), 部门(dapart)
private String id;
private String name;
private String depart;
public Employee() {
super();
}
public Employee(String id, String name, String depart) {
this.id = id;
this.name = name;
this.depart = depart;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show() {
System.out.print(depart+"的:"+ name);
System.out.println(",员工编号:"+id);
}
}
class Manager extends Employee{
Clerk c ;
public Manager() {
}
public Manager(String id, String name, String depart) {
super(id, name, depart);
}
public Clerk getC() {
return c;
}
public void setC(Clerk c) {
this.c = c;
}
public void show() {
super.show();
System.out.println("他的职员是"+ c.getName());
}
}
class Clerk extends Employee{
Manager m;
public Clerk() {
}
public Clerk(String id, String name, String depart) {
super(id, name, depart);
}
public Manager getM() {
return m;
}
public void setM(Manager m) {
this.m = m;
}
public void show() {
super.show();
System.out.println("他的经理是"+ m.getName());
}
|
|