matlab 面向对象学习笔记(二)

1、使用程序的方式进行GUI编程

main.m
% 构造初始数据
balance=500;
input=0;
% 构建figure对象和控件
hfig=figure('pos',[100,100,300,300]);
withdrawButton=uicontrol('parent',hfig,'string','withdraw','pos',[60 28 60 28]);
depositButton =uicontrol('parent',hfig,'string','deposit','pos',[180 28 60 28]);
inputBox      =uicontrol('parent',hfig,'style','edit','pos',[60 85 180... 28],'string',num2str(input),'Tag','inputbox';

balanceBox    =uicontrol('parent',hfig,'style','edit','pos',[180 142 60... 28],'string',num2str(balance),'Tag','balancebox';
textBox=uicontrol('parent',hfig,'style','text','string','Balance','pos',[60 142 60 28]);
% 注册回调函数
set(withdrawButton,'callback',@(o,e)withdraw_callback(o,e));% o为发布者 e为事件
% 设计回调函数
function withdraw_callback(o,e)
  hfig=get(o,'Parent'); % 获取事件发布者对象句柄
  inputBox=findobj(hfig,'Tag','inputbox');  % 找到input对象
  input=str2double(get(inputBox,'string')); % 获取input对数据
  balanceBox=findobj(hfig,'Tag','balancebox');
  balance=str2double(get(balanceBox,'string'));
  balance=balance-input;
  set(balanceBox,'string',num2str(balance));
end

步骤总结:

① 由脚本程序负责构造figure对象和控件对象

② 由脚本程序负责给控件注册回调函数

③ 设计独立的matlab函数作为回调函数

特点:相较于GUIDE设计简洁,需要设计详细位置,针对简单问题,使用方便,能够应对界面较少的情形。

2、如何利用面向对象的方式进行GUI编程?

模型-视图-控制器模式(Model-View-Controller),即MVC。模型(model):负责程序的内在逻辑,视图:view(负责构造和展示用户界面),控制器(controller):负责处理用户输入,针对用户在view界面的操作,controller首先做出回应,调用model中的函数,model状态发生变化,因此model是事件发布者,notify View对象,view对象接到通知,查询model的内在状态更新界面,view是观察者,而controller是监听者。

上例中main.m可以分为两部分:

① model(程序的中心逻辑)

balance=balance-input;    balance=balance+input;

② view(user interface,figure和控件对象,注册回调函数将界面与回调函数联系起来)

hfig=figure(.......); withdrawButton=uicontrol(.....);......

model相对稳定,界面要易于调整,view的调整不应影响model

针对此例,model作为发布者,需要定义事件和发布事件、定义核心逻辑

classdef Model < handle
   properties
      balance
   end
   events
      balanceChanged   % 定义事件
   end
   methods
    function obj=Model(balance)
        obj.balance=balance;
    end
    function deposit(obj,val)
       obj.balance=obj.balance+val; % 改变状态
       obj.notify('balanceChanged'); % 发布事件
    end
    function withdraw(obj,val)
       obj.balance=obj.balance-val; % 改变状态
       obj.notify('balanceChanged'); % 发布事件
    end
   end
end

view作为观察者,需要注册listener观察model状态变化并更新界面,首先要构造界面,要链接controller对用户在界面的操作做出解释,controller再调用model

classdef View < handle
    properties
       viewsize      ; 
       hfig          ;
       drawButton    ;
       depositButton ;
       balanceBox    ; 
       numBox        ; % 输入
       text          ;
       modelObj      ; % 模型对象句柄
       controlObj    ; % 控制器对象句柄
    end
    properties(Dependent) % 经常更新
       input;
    end
    methods
       function obj=View(modelObj)
          obj.viewsize=[100 100 300 200];
          obj.modelObj=modelObj;
          obj.modelObj.addlisterner('balanceChanged',@obj.updateBalance);
          obj.buildUI();
          obj.controlObj=obj.makeController();
          obj.attachToController(obj.controlObj);
       end
       function input=get.input(obj)
            input=get(obj.numBox,'string');
            input=str2double(input);
       end
       function buildUI(obj)
             obj.hfig=figure('pos',obj.viewsize);
             obj.drawButton=

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值