フィルターのクリア

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

7 ビュー (過去 30 日間)
Raymond
Raymond 2012 年 4 月 2 日
編集済み: Adam 2018 年 3 月 21 日
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.

採用された回答

Kye Taylor
Kye Taylor 2012 年 4 月 2 日
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 件のコメント
Walter Roberson
Walter Roberson 2012 年 4 月 2 日
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.
Raymond
Raymond 2012 年 4 月 3 日
thanks guys!

サインインしてコメントする。

その他の回答 (1 件)

Hadar
Hadar 2018 年 3 月 21 日
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 件のコメント
Adam
Adam 2018 年 3 月 21 日
編集済み: Adam 2018 年 3 月 21 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by