How are method names with a dot in them accessed in classes?
Show older comments
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?
Accepted Answer
More Answers (1)
David Young
on 25 Feb 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 Comments
Jiro Doke
on 25 Feb 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
on 26 Feb 2011
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!