matlab - Matlab OOP 中构造调用的顺序是什么?

标签 matlab oop constructor

我正在使用一个子类和 super 类,看起来与此类似:

classdef ClassSub < ClassSuper
    properties
        prop2
    end

    methods
        function self = ClassSub(Param1, Param2)
            self = ClassSuper(Param1);
            self.prop2 = Param2;
        end
    end
end

classdef ClassSuper
    properties
        prop1
    end

    methods
        function self = ClassSuper(Param1)
            self.prop1 = Param1;
        end
    end
end

当我去创建一个新的子类时:test = ClassSub(1,2); 我收到以下错误:

Not enough input arguments.

当我单步执行代码时,我注意到在调用子类的构造函数之前,先调用父类,使用零输入参数,然后调用子类构造函数,最后调用 super 的再次被调用。子类的正常构造调用顺序应该是什么?如果是这样,有没有办法先强制子类的构造函数,然后由谁调用父类?

最佳答案

call superclass constructor 的正确语法是:

classdef ClassSub < ClassSuper
    %# ...
    methods
        function self = ClassSub(Param1, Param2)
            self = self@ClassSuper(Param1);
            self.prop2 = Param2;
        end
    end
end

关于matlab - Matlab OOP 中构造调用的顺序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429997/

相关文章:

R 等效于 MATLAB 的 fmincon 用于约束优化?

java - 如何在 Java 中参数化响应解析?

c++ - 强制派生类使用基类的构造函数

Matlab:将char单元格转换为 double 向量单元格

matlab - 使用 MATLAB 绘制三元相图

Java:使用 super 调用时隐藏父类(super class)字段的值是多少?

language-agnostic - 在类层次结构的中间有空类是否有代码味?

python - Python 是否有类的默认构造函数?

java - 初学者 Java 构造函数中不需要参数错误

matlab - 在 Matlab 中对 float 进行位修改