标签:int method pac import pack att ring opened spl
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; public interface CalculateFactory { Calculate create(); }
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; import designpattern.staticfactorymethod.Plus; public class PlusFactory implements CalculateFactory{ @Override public Calculate create() { return new Plus(); } }
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; import designpattern.staticfactorymethod.Minus; public class MinusFactory implements CalculateFactory { @Override public Calculate create() { return new Minus(); } }
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; import designpattern.staticfactorymethod.Multiply; public class MultiplyFactory implements CalculateFactory { @Override public Calculate create() { return new Multiply(); } }
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; import designpattern.staticfactorymethod.Divide; public class DivideFactory implements CalculateFactory { @Override public Calculate create() { return new Divide(); } }
package designpattern.staticfactorymethod; public abstract class Calculate { public double num1; public double num2; Calculate() { } Calculate(double num1, double num2) { this.num1 = num1; this.num2 = num2; } public abstract double calculate(); }
加:
package designpattern.staticfactorymethod; public class Plus extends Calculate { public Plus() { } Plus(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 + num2; } }
package designpattern.staticfactorymethod; public class Minus extends Calculate { public Minus() { } Minus(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 - num2; } }
package designpattern.staticfactorymethod; public class Multiply extends Calculate { public Multiply() { } Multiply(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 * num2; } }
package designpattern.staticfactorymethod; public class Divide extends Calculate { public Divide() { } Divide(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 / num2; } }
package designpattern.factorymethod; import java.util.Scanner; import designpattern.staticfactorymethod.Calculate; public class Client { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个数字"); double num1 = scanner.nextDouble(); System.out.println("请输入一个运算符:+、-、*、/"); String operator = scanner.next();// 不能用nextLine(),会把上一个回车给吸收 System.out.println("请输入一个数字"); double num2 = scanner.nextDouble(); CalculateFactory calculateFactory = null; switch (operator) { case "+": calculateFactory = new PlusFactory(); break; case "-": calculateFactory = new MinusFactory(); break; case "*": calculateFactory = new MultiplyFactory(); break; case "/": calculateFactory = new DivideFactory(); break; default: break; } Calculate calculate = calculateFactory.create(); calculate.num1 = num1; calculate.num2 = num2; System.out.println(calculate.calculate()); scanner.close(); } }
标签:int method pac import pack att ring opened spl
原文地址:https://www.cnblogs.com/imoqian/p/10679343.html