Fork me on GitHub

Java从零开始学十七(简单工厂)

简单工厂的实现

实现一个计算器:要求输入2个数,和运算符,得到结果

Operation类

package com.pb.demo1;

public class Operation {
    private double num1;
    private double num2;
    
    public double getResult(double num1,double num2){
        double result=0;
        return result;
    }

    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;
    }
    
}

加,减,乖,除,取模

package com.pb.demo1;
/*
 * 加法
 */
public class AddOperation extends Operation {

    @Override
    public double getResult(double num1, double num2) {
        
        return (num1+num2);
    }

}
package com.pb.demo1;
/*
 * 减法
 */
public class Substruction extends Operation {

    @Override
    public double getResult(double num1, double num2) {
    
        return (num1-num2);
    }

    
}
package com.pb.demo1;
/*
 * 乖法
 */
public class Multiplication extends Operation {

    @Override
    public double getResult(double num1, double num2) {
        
        return (num1*num2);
    }
    

}
package com.pb.demo1;
/*
 * 除法
 */
public class Division extends Operation {

    @Override
    public double getResult(double num1, double num2) {
        
        return (num1/num2);
    }

    
}
package com.pb.demo1;
/*
 * 取余
 */
public class ModeOperation extends Operation {

    @Override
    public double getResult(double num1, double num2) {
        
        return (num1%num2);
    }
    

}

工厂类:

package com.pb.demo1;
/*
 * 简单工厂类
 */
public class Factory {
   //返回类型 为类对象
    public Operation getResult(String op){
        Operation operation=null;
        //根据操作符来声明对象并返回
        switch (op){
        case "+":
            operation=new AddOperation();
            break;
        case "-":
            operation=new Substruction();
            break;
        case "*":
            operation=new Multiplication();
            break;
        case "/":
            operation=new Division();;
            break;
        case "%":
            operation=new ModeOperation();
            break;
            default:
                System.err.println("输入运算符不正确!");
                break;
        }
        //返回生成的对象
        return operation;
    }
}

测试类:

package com.pb.demo1;

import java.util.InputMismatchException;
import java.util.Scanner;

/*
 * 计算器
 */
public class Demo1 {

    public static void main(String[] args) {
        //声明扫描器类型变量
        Scanner  input =new Scanner(System.in);
        //声明工厂对象
        Factory fac=new Factory();
    try{
        System.out.println("请输入第一个整数:");
        double num1=input.nextDouble();
        System.out.println("请输入第二个整数:");
        double num2=input.nextDouble();
        System.out.println("请输运算符:+、-、*、/、%");
        String op=input.next();
        //接收工厂的返回对象 工厂根据操作符生成的对象
        Operation operation=fac.getResult(op);
        //接收计算结果
            double result=operation.getResult(num1, num2);
            //显示计算结果
            System.out.println(num1+"与"+num2+"的运行结果:"+result);
    }catch(InputMismatchException e){
        System.err.println("输入类型不正确!");
    
    }catch(Exception e){
        e.printStackTrace();
    }
    }
}
posted @ 2015-02-18 18:40  森林森  阅读(509)  评论(0编辑  收藏  举报