/**
*
*/
public abstract class OperationBean {
private double _numberA = 0;//字段,即属性
private double _numberB = 0;
public double get_numberB() {
return _numberB;
}
public void set_numberB(double _numberB) {
this._numberB = _numberB;
}
public double get_numberA() {
return _numberA;
}
public void set_numberA(double _numberA) {
this._numberA = _numberA;
}
public abstract double GetResult();
}
public class OperationAdd extends OperationBean {
@Override
public double GetResult() {
double result = 0;
result = get_numberA() + get_numberB();
return result;
}
}
/**
*
*/
public class OperationSub extends OperationBean {
@Override
public double GetResult() {
double result = 0;
result = get_numberA() - get_numberB();
return result;
}
}
/**
*
*/
public class OperationMul extends OperationBean {
@Override
public double GetResult() {
double result = 0;
result = get_numberA() * get_numberB();
return result;
}
}
/**
*
*/
public class OperationDiv extends OperationBean {
@Override
public double GetResult() {
double result = 0;
result = get_numberA() / get_numberB();
return result;
}
}
/**
* 工厂类
*/
public class OperationFactory{
public static OperationBean createOperation(String operate){
OperationBean operation = null;
switch (operate){
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationSub();
break;
}
return operation;
}
}
/**
* 客户端代码
*/
public class TestUtil {
public static void main(String[] args) {
OperationBean operation = OperationFactory.createOperation("+");
operation.set_numberA(1);
operation.set_numberB(2);
System.out.println(operation.GetResult()+"");;
}
}