如何从Matlab中的超类继承文档?

时间:2013-05-07 15:58:08

标签: matlab oop inheritance subclass superclass

我有一个超级课程,我已经完成了一些广泛的文档。有继承自这个超类的子类,如果可能的话我想重用super的文档。例如,使用超类ClassA

classdef ClassA
    %CLASSA Super Class for all others classes
    %
    % CLASSA Properties:
    %   Prop1       It's the first property
    %   Prop2       It's the second
    %
    % CLASSA Methods:
    %   Method1     It's a method
    %   Method2     It's another method

    function value = Method1(var)
        % Super implementation of Method1
    end

    % Other method definitions follow
end

一个子类,ClassB:

classdef ClassB < ClassA
    %CLASSB Subclass of super class CLASSA
    %
    % CLASSB Properties:
    %   Prop3       It's the first property of subclass
    %   
    % CLASSB Methods:
    %   Method 3    It's the first method of subclass

    function value = Method1(var)
        % Subclass implementation of Method1
    end

    % Other method definitions follow
end

如果我输入help ClassB,我只会获得ClassB的帮助说明。我想要包括Super的帮助描述。输出看起来像这样:

 CLASSB Subclass of super class CLASSA

 CLASSB Properties:
    Prop1       It's the first property
    Prop2       It's the second
    Prop3       It's the first property of subclass

 CLASSB Methods:
    Method1     It's a method
    Method2     It's another method
    Method3     It's the first method of subclass

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:5)

正如@SamRoberts所述,文档属性和方法有standard way

例如:

classdef Super
    %Super Summary of this class goes here
    %   Detailed explanation goes here
    %
    % Super Properties:
    %    One - Description of One
    %    Two - Description of Two
    %
    % Super Methods:
    %    myMethod - Description of myMethod
    %

    properties
        One     % First public property
        Two     % Second public property
    end
    properties (Access=private)
        Three   % Do not show this property
    end

    methods
        function obj = Super
            % Summary of constructor
        end
        function myMethod(obj)
            % Summary of myMethod
            disp(obj)
        end
    end
    methods (Static)
        function myStaticMethod
            % Summary of myStaticMethod
        end
    end

end

classdef Derived < Super
    %Derived Summary of this class goes here
    %   Detailed explanation goes here
    %
    % See also: Super

    properties
        Forth     % Forth public property
    end

    methods
        function obj = Derived
            % Summary of constructor
        end
        function myOtherMethod(obj)
            % Summary of myMethod
            disp(obj)
        end
    end

end

现在在命令行上,您可以说:

help_super

您可以获得格式良好的超链接。

还要注意doc Derived的内容(或突出显示该单词并按 F1 )。这内部调用help2html将帮助转换为HTML。例如,我们可以自己做:

>> %doc Derived
>> web(['text://' help2html('Derived')], '-noaddressbox', '-new')

web

请注意,将显示继承的属性/方法。

答案 1 :(得分:2)

如果您按照描述的方式编写文档,我认为您可以获得所要求的唯一方法是重载help来执行自定义操作。例如,您重载的help可以在其自身上调用builtin('help'),然后在其超类上调用builtin('help')

但是,你不是以标准方式记录事物;通常,您可以使用属性本身上方的注释来记录属性,并在方法的函数签名下方注释带有注释的方法。如果您这样做,那么您将自动显示所有继承方法的帮助。