第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

簡單工廠模式

標(biāo)簽:
深度學(xué)習(xí)

一、简单工厂模式:

实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承自一个父类或接口)的实例。简单工厂模式的创建目标,所有创建的对象都是充当这个角色的某个具体类的实例。

二、场景

使用Java面向对象语言实现一个计算机控制台程序,要求输入两个数和运算符号,得到结果。
为了使程序达到易扩展,易维护,易复用且灵活的目的,本程序将使用到面向对象编程思想的三大特性:封装、继承和多态,以及简单工厂模式

三、程序代码

程序共有七个类:
这里写图片描述

MyOperation.java

public class MyOperation {

    private double numberA;
    private double numberB;
    public double getNumberA() {
        return numberA;
    }
    public void setNumberA(double numberA) {
        this.numberA = numberA;
    }
    public double getNumberB() {
        return numberB;
    }
    public void setNumberB(double numberB) {
        this.numberB = numberB;
    }    public double getResult() throws Exception {
        double result=0;
        return result;
    }
}

OperaAdd.java

public class OperaDiv extends MyOperation{

    @Override
    public double getResult() throws Exception {
        if(this.getNumberB()==0){
            throw new Exception("除数不能为0");
        }
        return this.getNumberA()/this.getNumberB();
    }
}

OperaSub.java

public class OperaDiv extends MyOperation{

    @Override
    public double getResult() throws Exception {
        if(this.getNumberB()==0){
            throw new Exception("除数不能为0");
        }
        return this.getNumberA()/this.getNumberB();
    }
}

OperaMul.java

public class OperaDiv extends MyOperation{

    @Override
    public double getResult() throws Exception {
        if(this.getNumberB()==0){
            throw new Exception("除数不能为0");
        }
        return this.getNumberA()/this.getNumberB();
    }
}

OperaDiv.java

public class OperaDiv extends MyOperation{

    @Override
    public double getResult() throws Exception {
        if(this.getNumberB()==0){
            throw new Exception("除数不能为0");
        }
        return this.getNumberA()/this.getNumberB();
    }
}

OperationFactory.java

public class OperationFactory {

    public static MyOperation createOperation(String operate){
        MyOperation operation=null;
        switch (operate) {
        case "+":
            operation=new OperaAdd();
            break;

        case "-":
            operation=new OperaSub();
            break;

        case "*":
            operation=new OperaMul();
            break;

        case "/":
            operation=new OperaDiv();
            break;
        default:
            operation=new MyOperation();
            break;
        }
        return operation;
    }
}

Calculator.java

import java.util.Scanner;public class Calculator {

    /**
     * @Title: main
     * @Description: 计算器
     * @param args   
     * @return void    返回类型
     * @throws
     */
    public static void main(String[] args) {
        MyOperation operation=null;        try {
            System.out.println("请输入数字A:");            double numberA=Double.valueOf(new Scanner(System.in).nextLine());
            System.out.println("请输入运算符:");
            String operaString=new Scanner(System.in).nextLine();
            System.out.println("请输入数字B:");            double numberB=Double.valueOf(new Scanner(System.in).nextLine());
            operation=OperationFactory.createOperation(operaString);
            operation.setNumberA(numberA);
            operation.setNumberB(numberB);
            System.out.println("结果是:"+operation.getResult());

        } catch (NumberFormatException e) {
            System.out.println("只能输入数字");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


    }

}

原文出处

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消