Effacer les filtres
Effacer les filtres

calling one method from a Static method

36 vues (au cours des 30 derniers jours)
Michaël
Michaël le 15 Mar 2016
Commenté : Michaël le 16 Mar 2016
Hello,
Here is the class definition:
classdef first < handle
methods (Static)
function hello
obj_first.bye
end
end
methods
function bye(obj)
disp('that works')
end
end
end
Here are my commands:
>> obj_first=first;
>> obj_first.bye
that works
>> obj_first.hello
Undefined variable "obj_first" or class "obj_first.bye".
Error in first.hello (line 4)
obj_first.bye
>>
Can you please help me?
Thank you very much

Réponse acceptée

Guillaume
Guillaume le 15 Mar 2016
Static methods apply to the class itself rather than instances (objects) of the class.You normally call such a method with classname.methodname, (i.e. in your case with first.hello). Matlab allows to call static methods from instances just so you don't have to determine the class of the object yourself, but other than determining which class the method belongs to, the object itself plays no role in the invocation of a static method. It is not passed to the method.
I don't understand why you thought your code would work. There's no obj_first in the scope of the static method, and even if somehow you made it able to access the variable from another workspace, there's no guarantee that such a variable exists.
More importantly, I don't understand what you're trying to achieve with your static method here. Can you explain what problem you're trying to solve?
One simple way to fix your code is to change the static method, so it takes an input:
function hello(some_obj)
some_obj.bye;
end
and you would then call it with:
obj_first.hello(obj_first)
But the whole lot would be pointless. You may as wall call the object method directly.
Another way to solve this, would be to create a singleton obj_first within the class but I've no idea if that's what you meant to do:
classdef first < handle
methods (Static)
function hello
obj_first = getsingleton();
obj_first.bye();
end
function obj_first = getsingleton()
persistent obj_first;
if isempty(obj_first)
obj_first = first();
end
end
end
methods
function bye(obj)
disp('that works')
end
end
end
>>obj_first = first.getsingleton;
>>obj2 = first();
>>first.hello();
  4 commentaires
Guillaume
Guillaume le 16 Mar 2016
Please use comments rather than answers.
Sorry I should have tested the code. I just learnt something new, you can't use a persistent variable as a return variable. To fix the persistent error, simply use a different variable name for the return variable and assign to it at the end of the function:
function o = getsingleton()
persistent obj_first;
if isempty(obj_first)
obj_first = first();
end
o = obj_first;
end
However, now that I know more about what you're doing, you're not interested in a singleton, so the above is the wrong approach anyway.
If you want your callback method to use some properties of instances of the class in which it is declared then that callback cannot be static. It has to be a normal method of the class and the callback will receive three arguments as I've shown you in your other question: the class object, the event source, and the event argument. E.g.:
classdef RespondToToggle < handle
properties
id; %for demo
end
methods
function obj = RespondToToggle(toggle_button_obj)
obj.id = randi([1 1e6]); %for demo
addlistener(toggle_button_obj,'ToggledState',@obj.handleEvnt);
end
function handleEvnt(obj, src,~)
disp(['in handleEvnt of ', num2str(obj.id)]);
if src.State
disp('ToggledState is true')
else
disp('ToggledState is false')
end
end
end
end
>> t = ToggleButton;
>> rtt1 = RespondToToggle(t)
rtt1 =
RespondToToggle with properties:
id: 704033
>> rt2 = RespondToToggle(t)
rtt2 =
RespondToToggle with properties:
id: 16187
>> t.OnStateChange(true)
in handleEvnt of 16187
ToggledState is true
in handleEvnt of 704033
ToggledState is true
Michaël
Michaël le 16 Mar 2016
Thank you!
There, I don't use Static methods.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by