MATLAB OOP - calling an object method from within another object method
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
i created two objects from two different classes. how do i call one object from within the other?
any advice would be very much appreciated. thanks.
0 commentaires
Réponse acceptée
Kye Taylor
le 2 Avr 2012
Let your two classes be class A and class B. Inside a property block of the class definition file for class A, include a variable, say bObj, that you intend to be an instance of class B. Then, in the constructor of class A, instantiate this instance of class B
bOjb = B(...)
Assuming that method m() of class B is public, invoke it anywhere in class definition file for class A with
bObj.m(...)
3 commentaires
Walter Roberson
le 2 Avr 2012
That's why Kye said "Assuming that method m() of class B is public". If they _are_ public, then just use what Kye showed. If they are not public, you have difficulties.
Plus de réponses (1)
Hadar
le 21 Mar 2018
hi, if class A and B definitions (properties and methods) are in different files (each of them is in its own file), how can class A "see" B and instantiate it? I get an error of undefined.
1 commentaire
Voir également
Catégories
En savoir plus sur Construct and Work with Object Arrays 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!