package src;
public class CaculateBean {
private double a;
private double b;
private double result;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}
public class Add extends CaculateBean{
@Override
public double getResult() {
return this.getA()+this.getB();
}
}
public class Mul extends CaculateBean{
@Override
public double getResult() {
return this.getA()+this.getB();
}
}
public class Sub extends CaculateBean{
@Override
public double getResult() {
return this.getA()+this.getB();
}
}
public class Div extends CaculateBean{
@Override
public double getResult() {
return this.getA()+this.getB();
}
}
public class BeanFactory{
//制造加法对象
public CaculateBean createAddBean() {
return new Add();
}
//制造减法对象
public CaculateBean createSubBean() {
return new Sub();
}
//制造减法对象
public CaculateBean createMultiBean() {
return new Mul();
}
//制造减法对象
public CaculateBean createDivBean() {
return new Div();
}
}
public class Factory-method {
public static void main(String[] args) {
CaculateBean addBean = new BeanFactory().createAddBean();
addBean.setA(1);
addBean.setB(2);
System.out.println(addBean.getResult());
}
}