How are method names with a dot in them accessed in classes?
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
David Young
2011-2-25
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 个评论
Jiro Doke
2011-2-25
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
2011-2-26
类别
在 帮助中心 和 File Exchange 中查找有关 Handle Classes 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!