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

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

问题描述

作为一个例子,假设我创建了一个名为 Shape 的抽象类和两个名为 CircleRectangle 的子类,它们都实现了一个 (abstract) 方法称为 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.

我尝试过类似以下的方法:

I have tried something like the following:

形状.m:

classdef (Abstract) Shape < handle

    methods (Abstract)
        Draw(obj);
    end

end

圆.m:

classdef Circle < Shape

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

end

矩形.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.异构的,您可以从中派生以允许创建不同类的数组.

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.

这应该是您所需要的,但要对异构数组进行额外控制,您可以覆盖 matlab.mixin.Heterogeneous 的getDefaultScalarElement 方法 指定默认对象.这应该被抽象基类覆盖:

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天全站免登陆