0

struct 型のクラス プロパティに関数をアタッチすることは可能ですか? 使用目的:

% 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 に答える