How are method names with a dot in them accessed in classes?
Mostrar comentarios más antiguos
In the tutorial Implementing a Set/Get Interface for Properties, a class MyAccount is implemented as a subclass of HGSETGET with part of the SET method overloaded. This is done by adding a method set.AccountStatus. However, this method does not show up in the list of methods for MyAccount. Moreover, there does not seem to be any way of calling this method. For example, the following command sequence gets a mysterious error:
>> h = MyAccount(1234567,500,'open');
>> h.set.AccountStatus('frozen')
??? Index exceeds matrix dimensions.
Moreover, if SET is overloaded, this method has no effect on the behavior of MyAccount.
EDIT: An attempt to access set.AccountStatus within SET has strange effects. If this code is inserted in the class,
function obj = set(obj,varargin)
obj = obj.set.AccountStatus(varargin{2});
end
and
>> h.set.AccountStatus('frozen')
is run, then obj.set.AccountStatus(varargin{2}) actually calls set(obj,{}).
How are methods with dots in their names accessed? EDIT: Is it still possible to access methods like SET.ACCOUNTSTATUS if I modify SET?
Respuesta aceptada
Más respuestas (1)
David Young
el 25 de Feb. de 2011
For normal classes, the set/get methods are called automatically when you assign to or access a property:
h.AccountStatus = 'frozen';
calls the set.AccountStatus method.
If you inherit from hgsetget, then you should be able to do
set(h, 'AccountStatus', 'frozen');
to call set.AccountStatus instead.
2 comentarios
Jiro Doke
el 25 de Feb. de 2011
To add to David's answer, set/get methods are the only ones that are implemented with a dot in the method. When you set the value of the property or get the value, the appropriate set/get method gets called. No other methods can have a dot in the name.
Andrew Newell
el 26 de Feb. de 2011
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!