UML
申请
public class Request {private String type;private String content;private int num;public Request() {}public Request(String type, String content, int num) {this.type = type;this.content = content;this.num = num;}@Overridepublic String toString() {return "Request{" +"type='" + type + '\'' +", content='" + content + '\'' +", num=" + num +'}';}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}
}
经理
public class CommonManager extends Manager {public CommonManager(String name) {super(name);}&#64;Overridepublic void requestApplications(Request request) {if ("请假".equals(request.getType()) && request.getNum() <&#61; 2) {System.out.println("经理 " &#43; name &#43; " 准许 " &#43; request.toString());} else if (superior !&#61; null) {superior.requestApplications(request);}}
}
总监
public class Majordomo extends Manager {public Majordomo(String name) {super(name);}&#64;Overridepublic void requestApplications(Request request) {if ("请假".equals(request.getType()) && request.getNum() <&#61; 5) {System.out.println("总监 " &#43; name &#43; " 准许 " &#43; request.toString());} else if (superior !&#61; null) {superior.requestApplications(request);}}
}
总经理
public class GeneraManager extends Manager {public GeneraManager(String name) {super(name);}&#64;Overridepublic void requestApplications(Request request) {if ("请假".equals(request.getType())) {System.out.println("总经理 " &#43; name &#43; " 准许 " &#43; request.toString());} else if ("加薪".equals(request.getType()) && request.getNum() <&#61; 500) {System.out.println("总经理 " &#43; name &#43; " 准许 " &#43; request.toString());} else if ("加薪".equals(request.getType()) && request.getNum() > 500) {System.out.println("总经理 " &#43; name &#43; " 这个请求我再考虑考虑 " &#43; request.toString());}}
}
测试类
public class Main {public static void main(String[] args) {CommonManager commonManager &#61; new CommonManager("王经理");Majordomo majordomo &#61; new Majordomo("李总监");GeneraManager generaManager &#61; new GeneraManager("张总经理");commonManager.setSuperior(majordomo);majordomo.setSuperior(generaManager);Request request1 &#61; new Request("请假", "我感冒了要请假&#xff01;", 2);commonManager.requestApplications(request1);Request request2 &#61; new Request("请假", "我感冒了要请假&#xff01;", 3);commonManager.requestApplications(request2);Request request3 &#61; new Request("加薪", "我要加薪!!&#xff01;", 500);commonManager.requestApplications(request3);Request request4 &#61; new Request("加薪", "我要加薪!!&#xff01;", 1000);commonManager.requestApplications(request4);}
}
运行结果