计算器界面代码重构

计算器界面代码重构什么是软件开发过程中的重构?重构(Refactoring)——以改善代码质量为目的代码重写   使其软件的设计和架构更加合理   提高软件的扩展性和维护性

代码实现与代码重构不同——代码实现   按照设计编程实现,重心在于功能实现——代码重构   以提高代码质量为目的软件架构设计 区别: 代码实现时不考虑架构的好坏,只考虑功能的实现 代码重构时不能影响已实现的功能,只考虑架构的改善

 

 

 计算器界面代码重构

 QCalculatorUI.h

#ifndef _QCALCULATORUI_H_
#define _QCALCULATORUIH_

#include <QWidget>
#include <QPushButton>
#include <QLineEdit>

class QCalculatorUI : public QWidget
{
private:
    QLineEdit* m_edit;
    QPushButton* m_buttons[20];

    QCalculatorUI();
    bool construct();


public:
    static QCalculatorUI* NewInstance();
    void show();
    ~QCalculatorUI();

};

#endif // _QCALCULATORUI_H_

QCalculatorUI.cpp

#include "QCalculatorUI.h"

QCalculatorUI::QCalculatorUI(): QWidget(NULL,Qt::WindowCloseButtonHint) //此处QCalculatorUI就是作为顶层窗口存在的,虽然这个地方继承自QWidget,但是赋值为NULL,相当于它是没有父类的(但是实际上还是有的)。
                                                                        //将窗口中的最大化和最小化去掉
{
    //因为QLineEdit与QCalculatorUI以及QPushButton与QCalculatorUI是组合关系,那么就应该同生死,因此需要在构造函数对其定义。因为此处涉及到在堆上申请内存空间,因此需要
    //使用二阶构造

}

bool QCalculatorUI::construct()
{
    bool ret = true;
    const char* btnText[20] =
    {
        "7", "8", "9", "+", "(",
        "4", "5", "6", "-", ")",
        "1", "2", "3", "*", "<-",
        "0", ".", "=", "/", "C",
    };

    m_edit = new QLineEdit(this);

    if(m_edit != NULL)
    {
        m_edit->move(10,10);
        m_edit->resize(240,30);
        m_edit->setReadOnly(true);  //使QLineEdit只读
    }
    else
    {
        ret = false;
    }

    for(int i=0; (i<4) && ret; i++)
    {
        for(int j=0; (j<5) && ret; j++)
        {
            if(m_buttons[i*5 + j] != NULL)
            {
                m_buttons[i*5 + j] = new QPushButton(this);
                m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
                m_buttons[i*5 + j]->resize(40,40);
                m_buttons[i*5 + j]->setText(btnText[i*5 + j]);
            }
            else
            {
                ret = false;
            }
        }
    }

    return ret;
}

QCalculatorUI* QCalculatorUI::NewInstance()
{
    QCalculatorUI* ret = new QCalculatorUI();

    if((ret == NULL) || !(ret->construct()))
    {
        delete ret;
        ret = NULL;
    }

    return ret;
}

void QCalculatorUI::show()
{
    QWidget::show();
    this->setFixedSize(this->width(),this->height()); //固定窗口的大小
}
QCalculatorUI::~QCalculatorUI()
{

}

main.cpp

#include <QApplication>
#include "QCalculatorUI.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QCalculatorUI* cal = QCalculatorUI::NewInstance();
    int ret = 0;

    if(cal != NULL)
    {
        cal->show();
        ret = a.exec();
        delete cal; //当程序运行到最后时,将生成的cal对象释放掉。
    }

    return ret;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值