2

In Matlab, I have the two classes

classdef A < matlab.mixin.Heterogeneous
    properties
        a;
    end
    methods
        function obj = A(varargin)
           obj.a = 3;
        end
    end
end

and

classdef B < A
    properties
        b;
    end
    methods
        function obj = B(varargin)
            obj = obj@A(varargin);
            obj.b = 4;
        end
    end
end

I now try to initialize an array of length 2 of type B:

>> objarray(2) = B

objarray = 

  1x2 heterogeneous A (A, B)

  Properties:
    a

  Methods, Superclasses

Why does Matlab insist on making it of class A ? And how can I insist that it be of class B instead?

EDIT: Using the debugger it is apparent that Matlab never enters the constructor for B when creating objarray(1)

4

1 回答 1

2

我自己从未尝试过,但我相信它matlab.mixin.Heterogeneous有一个getDefaultScalarElement您可以自己实现/覆盖的方法,它将定义当您分配稍后的元素时如何回填数组的初始元素。有关更多信息,请参阅本文档页面的大约一半。

于 2013-09-04T15:28:00.597 回答