MATLAB OOP - calling an object method from within another object method

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.

 Akzeptierte Antwort

One way is via composition: (see http://en.wikipedia.org/wiki/Object_composition)
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 Kommentare

yup i tried and it worked like what u said..
but i have the methods of class A in separate files in the @A folder, similar for those of class B in @B folders..
so how do i call the methods of bOBJ within the methods of aOBJ?
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.
thanks guys!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Hadar
Hadar am 21 Mär. 2018

0 Stimmen

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 Kommentar

Adam
Adam am 21 Mär. 2018
Bearbeitet: Adam am 21 Mär. 2018
Please ask a new question and give more details rather than adding it as an 'Answer' to a 6 year old question.
I notice you did do this yesterday so have replied there now.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 2 Apr. 2012

Bearbeitet:

am 21 Mär. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by