How to store objects from different classes in one array and call the same function (definitions are different)?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello Everybody,
I have two subclasses:
- Line
- Arc
Their objects will be creating a path by appending them on their end. These have their own "plotCurve" methods. I want to store these objects in an arrayi in order such like:
path = [line1,line2,arc1,line3,arc2,arc3,line4,arc4,arc5...];
(I think "path" should be the object of superclass "Path")
when I code something like;
for i=1:size(path)
path(i).plotCurve;
hold on
end
the result should be show. I should be able to see the whole path. (So, when the object of "Line" comes "plotCurve" method should be run and same for Arc objects).
Thanks in advance,
댓글 수: 0
답변 (1개)
Steven Lord
2020년 7월 30일
See this documentation page for how to implement your classes so you can create a heterogeneous array.
By the way, I recommend you avoid using path as a variable name. It already has a meaning in MATLAB.
댓글 수: 0
참고 항목
카테고리
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!