How can I combine two objects of different subclasses into the property of another class?
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello,
I explain the example so that it is better understood: I have a "board" class with a "player" attribute. This attribute is a list of different objects of the "human player" class. If I want to add to this list "player" objects of the class "player cpu" I get an error.
댓글 수: 0
채택된 답변
Benjamin Kraus
2018년 5월 24일
Specifically, your player class needs to derive from matlab.mixin.Heterogeneous. Then, as long as all your other player classes derive from your main player base class, you should be all set.
추가 답변 (1개)
Philip Borghesani
2018년 5월 24일
Two ways to do this:
- Make player a cell array instead of an array of of human players. This is the simplest solution but may not be the best in the long run.
- Derive "human player" and "player cpu" from a common base class that is heterogeneous
댓글 수: 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!