0

是否可以将函数附加到结构类型的类属性?预期用途:

% Definition:
classdef a < handle
  properties
    bar
  end
  methods
    function obj = a()
      obj.bar = struct;
      %obj.bar.attachFunction('apply', @someFunction); <-- something like this
    end
  end
end

% Usage:
foo = a();
foo.bar.apply('test');
foo.bar.var1 = 1;
foo.bar.var2 = 2;
4

1 回答 1

0

哦,好吧,这实际上很简单,一旦我用了我的头脑。

classdef a < handle
  properties
    bar
  end
  methods
    function obj = a()
      obj.bar = struct;
      obj.bar.apply = @(str) @obj.barApply(str);
    end
  end
  methods (Access=protected)
    function barApply(obj, str)
      obj.bar.something = str;
    end
  end
end

foo = a();
foo.bar.apply('monkey');
foo.bar.apple = 2;
于 2016-12-27T09:09:53.573 回答