如何在MATLAB中创建抽象类对象的数组? [英] How do I create an array of abstract class objects in MATLAB?

查看:467
本文介绍了如何在MATLAB中创建抽象类对象的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个示例,假设我创建了一个名为Shape的抽象类以及两个名为CircleRectangle的子类,这两个子类均实现了称为Draw的(抽象)方法.我希望能够创建许多CircleRectangle对象,将它们存储在数组中,并通过遍历数组来在每个数组对象上调用Draw.

As an example, suppose I have created an abstract class called Shape and two subclasses called Circle and Rectangle that both implement an (abstract) method called Draw. I would like to be able to create a number of Circle and Rectangle objects, store them in an array and call Draw on each array object by iterating through the array.

我尝试了以下操作:

Shape.m:

classdef (Abstract) Shape < handle

    methods (Abstract)
        Draw(obj);
    end

end

Circle.m:

classdef Circle < Shape

    methods
        function obj = Draw(obj)
            disp('This is a circle');
        end
    end

end

Rectangle.m:

Rectangle.m:

classdef Rectangle < Shape

    methods
        function obj = Draw(obj)
            disp('This is a rectangle');
        end
    end

end

test.m:

shapes = Shape.empty();

myrect = Rectangle();
mycirc = Circle();

shapes(end + 1) = myrect;
shapes(end + 1) = mycirc;

for i = 1:size(shapes,1)
    shapes(i).Draw();
end

当我尝试运行test.m时,出现以下错误消息:

When I try to run test.m, I get the following error message:

Error using Shape.empty
Abstract classes cannot be instantiated.
Class 'Shape' defines abstract methods
and/or properties.

Error in test (line 1)
shapes = Shape.empty();

推荐答案

从错误中可以明显看出, matlab.mixin.Heterogeneous 从中可以派生出来以允许创建不同类的数组.

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.

首先,从Shape.m中的matlab.mixin.Heterogeneous派生:

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

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

然后在测试脚本中,从CircleRectangle初始化shapes:

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:

如果根类"是抽象的或不是异构层次结构中的类的适当默认对象,则覆盖此方法. getDefaultScalarElement必须返回异构层次结构中另一个成员的实例.

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.

假设您要对Shape派生的对象数组使用默认对象Circle:

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

现在,从Shape派生的对象数组中缺少的元素将被Circle对象填充:

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.

这篇关于如何在MATLAB中创建抽象类对象的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆