MATLAB OOP - calling an object method from within another object method
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
Kye Taylor
2012년 4월 2일
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
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.
추가 답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!