how do I dynamically set a method for a class?
6 ビュー (過去 30 日間)
古いコメントを表示
I have a class definition that should implement a "step" method. However, the "step" method will change depending on the mode; the mode values can be: "begin", "middle", "last." I don't want to use a switch/case inside the "step" because I already know what the function should and just want it called directly (that is, "step" may be called at 100Hz but the mode changes once every 30 seconds or 5 minutes, I don't want to evaluate a switch statement at 100Hz (.01 sec) when it will only need to be changed every 30 sec)
Below is a classdef and a way I tried to do this.
classdef cName < handle
methods
function step(this)
% step based on the mode
end
function stepBegin(this)
% do begin stepping stuff
end
function stepMiddle(this)
% do middle stepping stuff
end
function stepLast(this)
% do last stepping stuff
end
% I don't like this, doesn't work the way I'd expect.
function setStep(this, inMode)
switch inMode
case 'begin'
this.step = @this.stepBegin;
case 'middle'
this.step = @this.stepMiddle;
case 'last'
this.step = @this.stepLast;
end
I have tried the code above but when "step" is called, it always goes to "setStep," goes thru the switch and then jumps to the function handle. I don't like this and am wondering if there is a better way. Is there a proper "class" way to implement this behavior?
BTW, I have tried writing separate functions in separate script files and then setting a function handle to these functions. This works fast but I have a bunch of function files all over the place and I would like for the function to just be methods of the class.
classdef cName < handle
properties
stepFunctionHandle
end
methods
function step(this)
this.stepFunctionHandle(this);
end
% I don't like keeping track of all the files everywhere; that's what my class def is for!!
function setStep(this, inMode)
switch inMode
case 'begin'
this.stepFunctionHandle= @separateStepBegin;
case 'middle'
this.stepFunctionHandle= @separateStepMiddle;
case 'last'
this.stepFunctionHandle= @separateStepLast;
end
Addition: Using a property as a function handle, you can set it to a function defined in a classdef. I think my original problem was caused by my initialization of the function handle. I initialized the function like an anonymous function, ie., "@(varlist)functionHandle". so it jump to the definition in the switch statement and then the method in the class. this is not needed.
This seems to work just fine:
classdef cName < handle
properties
stepFunctionHandle
end
methods
function step(this)
this.stepFunctionHandle(this);
end
function setStep(this, inMode)
switch inMode
case 'begin'
this.stepFunctionHandle= @stepBegin;
case 'middle'
this.stepFunctionHandle= @stepMiddle;
case 'last'
this.stepFunctionHandle= @stepLast;
end
function stepBegin(this)
end
function stepMiddle(this)
end
function stepStart(this)
end
end
end
0 件のコメント
回答 (1 件)
Matt J
2016 年 5 月 31 日
編集済み: Matt J
2016 年 5 月 31 日
BTW, I have tried writing separate functions in separate script files and then setting a function handle to these functions. This works fast but I have a bunch of function files all over the place
It is not necessary to put these functions in separate files if that is the only thing that is bothering you. You can put them in your classdef file as sub-functions rather than class methods, as sketched below. Subfunctions placed in a classdef file are visible to the class only.
%%%Everything below goes in classdef file cName.m
classdef cName<handle
end
function separateStepBegin()
end
function separateStepMiddle()
end
function separateStepEnd()
end
Another more object-oriented alternative is to have sub-classes for each of your modes:
%%%These go in separate classdef mfiles
classdef beginMode<cName
end
classdef middleMode<cName
end
classdef endMode<cName
end
In each sub-class, you would overload the step() method appropriately. You could also have Converter Methods to convert for example an object of sub-class beginMode to an object of sub-class middleMode and so forth.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Software Development Tools についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!