How do I get a subclass to listen to an event generated in a superclass?

4 vues (au cours des 30 derniers jours)
Etaoin Shrdlu
Etaoin Shrdlu le 12 Juin 2015
Réponse apportée : Spruha le 16 Juin 2025
I have a superclass, BasicPlot that generates an event 'DonePlotting' when the main plotting method is finished: notify(s, 'DonePlotting', event.EventData), where 's' is the object handle returned by the BasicPlot constructor.
A subclass FancyPlot < BasicPlot has a listener in its constructor that's supposed to be triggered by 'DonePlotting': s.hLsnr = addlistener(s, 'DonePlotting', 'PostSet', @(src, evt)fancyplot_event(s, src, evt));
However, when it gets to that line, Matlab reports, "The name 'DonePlotting' is not an accessible property for an instance of class 'FancyPlot'.
When I stop the program in the debugger just before the addlistener line and ask for 'events(s)', it lists 'DonePlotting'. Hum...
So, how do I get the subclass to listen to an event generated by the superclass?

Réponses (1)

Spruha
Spruha le 16 Juin 2025
Hi Etaoin,
The issue you're encountering is due to the incorrect usage of the 'PostSet' argument. You're trying to listen for an event, but you are using the syntax for a property listener.
This results in the following error:
"The name 'DonePlotting' is not an accessible property for an instance of class 'FancyPlot'."
This is because 'PostSet' is trying to find a property named DonePlotting. However, it is an event, not a property.
You can refer to the below code samples:
  1. For events -
addlistener(obj, 'EventName', callback)
2. For properties -
addlistener(obj, 'PropertyName', 'PostSet', callback)
Hope this helps!

Catégories

En savoir plus sur Events dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by