(X-5-7)已知某企业的采购审批是分级进行的,根据采购金额的不同由不同层次的主管人员来审批,主任可以审批5万元以下(不包括5元)的采购单,副董事长可以审批5万元至10万元(不包括10万元)的采购单,董事长可以审批10万元至50万元(不包括50万元)的采购单,50万元及以上的采购单就需要开会讨论决定。采用职责链设计模式对上述过程进行设计得到的类图如下图所示。[img=813x391]17e0c93c1051c77.png[/img]public PurchaseRequest{public double amount;//一个采购的金额public int number;//采购单编号public String purpose;//采购目的}//审批者类class Approver {public Approver() {successor = null;}public void processRequest(PurchaseRequest aRequest) {if( successor!= null){successor.(1);}}public void setSuccessor(Approver aSuccessor) {successor = aSuccessor;}private(2)successor;}class Congress extends Approver {public void processRequest(PurchaseRequest aRequest) {if (aRequest.amount >; 0000) {// 决定是否审批的代码省略}else(3).processRequest(aRequest);}}class Director extends Approver {public void processRequest(PurchaseRequest aRequest) {// 此处代码省略}}class President extends Approver {public void processRequest(PurchaseRequest aRequest) {// 此处代码省略}}class VicePresident extends Approver {public void processRequest PurchaseRequest aRequest){// 此处代码省略}}public class Test {public static void main(String[ args) throws IOException{Congress meeting=new Congress();VicePresident sam= new VicePresident();Director larry= new Director();//构造职责链meeting. setSuccessor(null);sam.setSuccessor((4)) ;tammy.setSuccessor((5));larry.setSuccessor((6 ));//构造一个采购审批请求BufferedReader br= new BufferedReader(new InputStreamReader(System. in));aRequest.amount=Double. parseDouble( br. readLine());(7).processRequest(aRequest);}}
举一反三
- (X-5-7)已知某企业的采购审批是分级进行的,根据采购金额的不同由不同层次的主管人员来审批,主任可以审批5万元以下(不包括5元)的采购单,副董事长可以审批5万元至10万元(不包括10万元)的采购单,董事长可以审批10万元至50万元(不包括50万元)的采购单,50万元及以上的采购单就需要开会讨论决定。采用职责链设计模式对上述过程进行设计得到的类图如下图所示。[img=813x391]17e44abe460ef53.png[/img]public PurchaseRequest{public double amount;//一个采购的金额public int number;//采购单编号public String purpose;//采购目的}//审批者类class Approver {public Approver() {successor = null;}public void processRequest(PurchaseRequest aRequest) {if( successor!= null){successor.(1);}}public void setSuccessor(Approver aSuccessor) {successor = aSuccessor;}private(2)successor;}class Congress extends Approver {public void processRequest(PurchaseRequest aRequest) {if (aRequest.amount >; 0000) {// 决定是否审批的代码省略}else(3).processRequest(aRequest);}}class Director extends Approver {public void processRequest(PurchaseRequest aRequest) {// 此处代码省略}}class President extends Approver {public void processRequest(PurchaseRequest aRequest) {// 此处代码省略}}class VicePresident extends Approver {public void processRequest PurchaseRequest aRequest){// 此处代码省略}}public class Test {public static void main(String[ args) throws IOException{Congress meeting=new Congress();VicePresident sam= new VicePresident();Director larry= new Director();//构造职责链meeting. setSuccessor(null);sam.setSuccessor((4)) ;tammy.setSuccessor((5));larry.setSuccessor((6 ));//构造一个采购审批请求BufferedReader br= new BufferedReader(new InputStreamReader(System. in));aRequest.amount=Double. parseDouble( br. readLine());(7).processRequest(aRequest);}}
- 给定Java代码如下所示,则编译运行后,输出结果是。 class Parent { public void count() { System.out.println(10%3); } } public class Child extends Parent{ public void count() { System.out.println(10/3); } public static void main(String args[]) { Parent p = new Child(); p.count(); } }
- 有如下类的定义。class A{ public void show() { show2(); } public void show2() { System.out.print("我"); }}class B extends A{ public void show2(){ System.out.print("爱"); }}class C extends B{ public void show() { super.show(); } public void show2() { System.out.print("你"); }}public class Test4 { public static void main(String[] args) { A a=new B(); a.show(); B b=new C(); b.show(); }} A: 我爱你 B: 爱你 C: 我爱 D: 我你
- import java.io.*; class Person{ public void print(){System.out.print("Person ");} public void printMyGender(String s){ this.print(); System.out.print(s+" "); } } class Gender{ String type="gender"; public void print(Person p){p.printMyGender(type);} } class Female extends Gender{ public Female(){ type="female"; } } class Male extends Gender{ public Male(){ type="male"; } } class Employee extends Person{ public void print(){ System.out.print("Employee ");} } class Manager extends Employee{ public void print(){ System.out.print("Manager ");} } public class Test{ public static void main(String[] args){ Manager man = new Manager(); Employee em = new Employee(); Gender gender1 = new Male(); Gender gender2 = new Female(); gender1.print(man); gender2.print(em); } } 对于以上代码,其运行结果是
- (7-1)以下程序的运行结果是( )。 class A{ A(){ System.out.print(10); } } public class Demo extends A { public static void main(String[] args) { new A(); new Demo(); } }