Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Matlab中用超类函数修改子类属性_Matlab_Oop_Inheritance - Fatal编程技术网

在Matlab中用超类函数修改子类属性

在Matlab中用超类函数修改子类属性,matlab,oop,inheritance,Matlab,Oop,Inheritance,我的子类与我的超类具有相同的属性。 如下所示 classdef superclass < handle properties a b c methods function sup = superclass(...) sup.create(...) end classdef subclass < superclass properties a1 b1

我的子类与我的超类具有相同的属性。 如下所示

classdef superclass < handle
    properties
      a
      b
      c
    methods
      function sup = superclass(...)
          sup.create(...)
      end

classdef subclass < superclass
    properties
      a1
      b1
      c1
    methods
      function sub = subclass(...)
然后(这就是我被卡住的地方)子类构造函数第二次初始化所有的值a1,b1,c1。 由于初始化属性的过程在sup和sub之间没有变化,因此我希望像这样重用它:

    function sub = subclass(args1, args2)
       sub@superclass(args1)
       sub.create(args2)

如何在不为子类编写新的“创建”函数的情况下实现这一点?

一种可能性是重构
create
方法以返回三个值(而不是硬编码属性),然后可以在超类和子类中调用它,如下所示:

[sup.a,sup.b,sup.c] = sup.create(...);

在哪里

并让
create
方法接收包含要填充的属性名的字符串单元格数组

[sup.a,sup.b,sup.c] = sup.create(...);
[sub.a1,sub.b1,sub.c1] = sub.create(args2);
classdef superclass < handle
    methods (Access = protected)
        function [x,y,z] = create(obj, args)
            x = ..; y = ...; z = ...;
        end
    end
end
propname = 'a';
obj.(propname) = 0;