MATLAB Answers


How can I combine two objects of different subclasses into the property of another class?

Ignacio Estaún さんによって質問されました 2018 年 5 月 24 日
最新アクティビティ Ignacio Estaún さんによって コメントされました 2018 年 5 月 25 日
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 件のコメント

サインイン to comment.




2 件の回答

回答者: Benjamin Kraus 2018 年 5 月 24 日

Your classes need to derive from the matlab.mixin.Heterogeneous class.
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.
Check out this documentation page for more details: Designing Heterogeneous Class Hierarchies

  1 件のコメント

Thanks, it worked perfectly!

サインイン to comment.

回答者: Philip Borghesani 2018 年 5 月 24 日

Two ways to do this:
  1. 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.
  2. Derive "human player" and "player cpu" from a common base class that is heterogeneous

  0 件のコメント

サインイン to comment.

Translated by