失效链接处理 |
创建型设计模式 PDF 下载
本站整理下载:
相关截图:
主要内容:
1.2客户端类ZC32JSQ
package jisuanqi;
import java.util.Scanner;
public class JSQ {
public static void main(String[] args) {
double result = 0;
//1.接收控制台输入
System.out.println("-----计算器程序-----");
System.out.println("输入第一个操作数");
Scanner scan = new Scanner(System.in);
String strNum1 = scan.nextLine();
System.out.println("输入运算符");
String operation = scan.nextLine();
System.out.println("输入第二个操作数");
String strNum2 = scan.nextLine();
double num1 = Double.parseDouble(strNum1);
double num2 = Double.parseDouble(strNum2);
Operation oper=OperationFactory.getResult(operation);
oper.setNum1(num1);
oper.setNum2(num2);
result=oper.getResult();
System.out.println(num1 + operation + num2 + "=" + result);
}
}
1.3抽象运算类zc32Operation
package jisuanqi;
public class Operation {
public double num1;
public double num2;
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
public double getResult() {
double result=0;
return result;
}
}
1.31具体运算类zc32AddOperation
package jisuanqi;
//加法类
public class AddOperation extends Operation{
public double getResult() {
double result=0;
result=(num1+num2);
return result;
}
}
1.32具体运算类zc32SubOperation
package jisuanqi;
//减法类
public class SubOperation extends Operation{
public double getResult() {
double result=0;
result=(num1-num2);
return result;
}}
1.33具体运算类zc32ChengOperation
package jisuanqi;
//乘法类
public class ChengOperation extends Operation{
public double getResult() {
double result=0;
result=(num1*num2);
return result;
}}
1.34具体运算类zc32ChuOperation
package jisuanqi;
//除法类
public class ChuOperation extends Operation{
public double getResult() {
double result=0;
result=(num1/num2);
return result;
}}
1.4抽象工厂类zc32OperationFactory
package jisuanqi;
interface OperationFactory { // 工厂接口
public static Operation getResult(String operator) {
Operation oper=null;
switch(operator) {
case"+":
oper=new AddOperation();
break;
case"-":
oper=new SubOperation();
break;
case"*":
oper=new ChengOperation();
break;
case"/":
oper=new ChuOperation();
break;
}
return oper;
}
}
|