调用类中的方法,对象发生冲突?

时间:2016-03-16 19:46:20

标签: matlab oop events

我有两个班级:

classdef ToggleButton < handle
   properties
      State = false
   end
   events
      ToggledState
   end
   methods
      function OnStateChange(obj,newState)
         if newState ~= obj.State
            obj.State = newState;
            notify(obj,'ToggledState');
         end
      end
   end
end

classdef RespondToToggle < handle
   methods
      function obj = RespondToToggle(toggle_button_obj)
         addlistener(toggle_button_obj,'ToggledState',@obj.handleEvnt);
      end
      function handleEvnt(~,src,~)
         if src.State
            disp('ToggledState is true')
            obj.hello
         end
      end
      function hello(obj)
          disp('that works')
      end
   end
end

所以我希望能够调用函数&#34;你好&#34;来自函数handleEvnt,因为它们都属于同一个类,RespondToToggle。

这是我得到的错误:

>> button=ToggleButton;
>> responder=RespondToToggle(button);
>> OnStateChange(button,1)
ToggledState is true
Warning: Error occurred while executing callback:
Undefined variable "obj" or class "obj.hello".

Error in RespondToToggle/handleEvnt (line 9)
            obj.hello

Error in RespondToToggle>@(varargin)obj.handleEvnt(varargin{:}) (line 4)
         addlistener(toggle_button_obj,'ToggledState',@obj.handleEvnt);

Error in ToggleButton/OnStateChange (line 12)
            notify(obj,'ToggledState');

> In ToggleButton>ToggleButton.OnStateChange at 12 

你能帮帮我吗?

谢谢

0 个答案:

没有答案