abstract class Operation {
double NumberA;
double NumberB;
public double getNumberA() {
return NumberA;
}
public double getNumberB() {
return NumberB;
}
public abstract double GetResult();
}
class OperationAdd extends Operation {
public double GetResult()
{
double result=0;
result=super.getNumberA()+super.getNumberB();
return result;
}
}
class OperationSub extends Operation {
public double GetResult()
{
double result=0;
result=super.getNumberA()-super.getNumberB();
return result;
}
}
class OperationMul extends Operation {
public double GetResult()
{
double result=0;
result=super.getNumberA()*super.getNumberB();
return result;
}
}
class OperationDiv extends Operation {
public double GetResult()
{
double result=0;
result=super.getNumberA()/super.getNumberB();
return result;
}
}
interface IFactory {
Operation CreateOperation();
}
class AddFactory implements IFactory {
public Operation CreateOperation()
{
return new OperationAdd();
}
}
class SubFactory implements IFactory {
public Operation CreateOperation()
{
return new OperationSub();
}
}
class MulFactory implements IFactory {
public Operation CreateOperation()
{
return new OperationMul();
}
}
class DivFactory implements IFactory {
public Operation CreateOperation()
{
return new OperationDiv();
}
}
public class Main {
public static void main(String args[])
{
IFactory operFactory=new AddFactory();
Operation oper=operFactory.CreateOperation();
oper.NumberA=1;
oper.NumberB=2;
double result=oper.GetResult();
System.out.println(result);
}
}
工厂方法
最新推荐文章于 2024-03-11 20:47:58 发布