How do I reference a super class of a superclass object?

I have a subclass of a superclass of a superclass. The two levels up superclass owns a property that has a set method defined. I'd like to have the subclass also act on the setting of the super-superclass property. I thought I'd use a property listener in the subclass that was listening to the super-superclass's property. The problem I'm having is syntax. What's the syntax for event.proplistener? How do I reference a super-super class instance or object? Or, do I need to?
I've tried: Obj.RecordNameListener = ... event.proplistener(Obj,'RecordName','PostSet',@Obj.OnRecordNameChanged);
Where Obj is the subclass object reference. But i really shouldn't be listening to myself I should be listening to my super-super class:
Obj@FirstLevelSuperClass@SecondLevelSuperClass doesn't work
Is this a good approach?
Thanks in advance.

 Réponse acceptée

I find that using the predefined set methods (i.e. function set.Foo(obj,value)) are convenient in simple scenarios, but for reasonably complex cases like this it is just easier to define your own setter methods. Then you'd call the method to set the property instead of accessing the property directly. Here's a simple example:
classdef SuperSuper < handle
properties
TheProperty
end
methods
function setTheProperty(self, val)
% Notice no dot in the function name
disp('SuperSuper setter!');
self.TheProperty = val;
end
end
end
classdef Super < SuperSuper
end
classdef Base < Super
methods
function setTheProperty(self, val)
setTheProperty@SuperSuper(self, val);
disp('Base setter!');
self.TheProperty = val;
end
end
end
Usage:
>> B = Base;
>> B.setTheProperty(5)
SuperSuper setter!
Base setter!
>> B.TheProperty
ans =
5
>>

3 commentaires

Thanks Patrick. That answers that. Just @WhateverClassYouWantToCall. I did get the property listener to work as well. My error was trying to pass a string indicating the property name rather than passing a meta property reference - the key was using findprop(). No TMW Docs had an example of this so there was some trial and error. This however works:
%---RecordName is defined in clsPart so we can't define a set
%---ftn for it in clsVehicle. So, we listen for when it changes
%---and set subsystem component's RecordNames when our record name changes.
RecordNameProp = findprop(Obj,'RecordName');
Obj.RecordNameListener = ...
event.proplistener(Obj,RecordNameProp,'PostSet',@Obj.OnRecordNameChanged);
When the RecordName property of the super super class changes, OnRecordNameChanged in the base class gets called as well.
I am not sure, but I don't think you want (or need to do this). I believe setTheProperty should be an inherited method of Super, therefore you can call setTheProperty@Super(self, val). The advantage of this is that if in the future Super overloads setTheProperty, Base will not skip it. Further, instead of defining your own setters, you can use subsref to intercept the call to set.TheProperty (notice the dot).
Sure, that's a viable way to do it, too.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by