Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
User interface 在MATLAB中,类方法可以作为uicontrol回调而不公开吗?_User Interface_Matlab_Callback_Oop_Access Control - Fatal编程技术网

User interface 在MATLAB中,类方法可以作为uicontrol回调而不公开吗?

User interface 在MATLAB中,类方法可以作为uicontrol回调而不公开吗?,user-interface,matlab,callback,oop,access-control,User Interface,Matlab,Callback,Oop,Access Control,在Matlab2008a中,是否有一种方法允许类方法充当uicontrol回调函数,而不必公开该方法?从概念上讲,该方法不应该是公共的,因为它永远不应该被类的用户调用。只能在UI事件触发回调时调用它。但是,如果我将方法的访问权限设置为private或protected,则回调将不起作用。我的类派生自hgsetget,并使用2008a classdef语法定义 uicontrol代码类似于: methods (Access = public) function this = MyClass

在Matlab2008a中,是否有一种方法允许类方法充当uicontrol回调函数,而不必公开该方法?从概念上讲,该方法不应该是公共的,因为它永远不应该被类的用户调用。只能在UI事件触发回调时调用它。但是,如果我将方法的访问权限设置为private或protected,则回调将不起作用。我的类派生自hgsetget,并使用2008a classdef语法定义

uicontrol代码类似于:


methods (Access = public)
   function this = MyClass(args)
      this.someClassProperty = uicontrol(property1, value1, ... , 'Callback', ...
      {@(src, event)myCallbackMethod(this, src, event)});
      % the rest of the class constructor code
   end
end
回调代码如下所示:


methods (Access = private)  % This doesn't work because it's private
   % It works just fine if I make it public instead, but that's wrong conceptually.
   function myCallbackMethod(this, src, event)
      % do something
   end
end

将回调函数句柄存储为私有属性似乎可以解决这个问题。试试这个:

classdef MyClass
    properties
        handle;
    end

    properties (Access=private)
        callback;
    end

    methods
        function this = MyClass(args)
            this.callback = @myCallbackMethod;
            this.handle = uicontrol('Callback', ...
                {@(src, event)myCallbackMethod(this, src, event)});
        end
    end

    methods (Access = private)
        function myCallbackMethod(this, src, event)
            disp('Hello world!');
        end
    end
end