How do I create an array of abstract class objects in MATLAB?

天涯浪子 提交于 2019-11-30 05:11:38

As is clear from the error, you cannot instantiate an abstract class (see sebastian's answer for details). However, there is a special superclass called matlab.mixin.Heterogeneous from which you can derive to allow creation of an array of different classes.

First, derive from matlab.mixin.Heterogeneous in Shape.m:

classdef (Abstract) Shape < handle & matlab.mixin.Heterogeneous

Then in your test script, initialize shapes from either Circle or Rectangle:

shapes = Circle.empty();

When you run the loop, the array will change class:

>> shapes

shapes = 

  1x2 heterogeneous Shape (Rectangle, Circle) array with no properties.

>> shapes(1)

ans = 

  Rectangle with no properties.

>> shapes(2)

ans = 

  Circle with no properties.

That should be all you need, but for additional control over a heterogeneous array, you can override the getDefaultScalarElement method of matlab.mixin.Heterogeneous to specify the default object. This should be overridden for abstract base classes:

Override this method if the Root Class is abstract or is not an appropriate default object for the classes in the heterogeneous hierarchy. getDefaultScalarElement must return an instance of another member of the heterogeneous hierarchy.

Say you want the default object to be Circle for an array of objects deriving from Shape:

methods (Static, Sealed, Access = protected)
    function default_object = getDefaultScalarElement
        default_object = Circle;
    end
end

Now missing elements in an array of objects derived from Shape will be filled with Circle objects:

>> clear r
>> r(2) = Rectangle
r = 
  1x2 heterogeneous Shape (Circle, Rectangle) array with no properties.
>> r(1)
ans = 
  Circle with no properties.
>> r(2)
ans = 
  Rectangle with no properties.

From the docs:

abstract class — A class that cannot be instantiated, but that defines class components used by subclasses.

See: Mathworks-Docs

Which is, afaik, the definition of abstract classes in other programming languages as well (someone correct me if I'm wrong).

So to construct an array that holds various kinds of Shape elements, I'd guess you'll either have to make Shape non-abstract or implement another non-abstract class, that all your real implementations inherit from.

EDIT: For completeness:

I tried what you're trying achieve and at first sight, object-arrays with mixed elements that have a common superclass don't exist:

>> objects(1) = Foo();
>> objects(2) = FooBar();
The following error occurred converting from FooBar to Foo:
Error using Foo
Too many input arguments.

>> FooBar

ans = 

  FooBar handle with no properties.
  Methods, Events, Superclasses


Superclasses for class FooBar:

    Foo
    handle

EDIT 2: See chappjc's solution for this issue ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!