5

我有一个 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

有没有办法做到这一点?

4

2 回答 2

5

正如@SamRoberts所指出的,有一种记录属性和方法的标准方法。

例如:

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')

网络

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

于 2013-05-07T19:45:57.807 回答
2

If you are writing documentation in the way you describe, I think the only way you can get what you're asking for is to overload help to do something customised. For example, your overloaded help could call builtin('help') on itself, and then builtin('help') on its superclass.

However, you're not documenting things in the standard way; typically you would document properties using comments immediately above the property itself, and document methods with comments immediately below the function signature of the method. If you did that, then you'd automatically display the help for all the inherited methods.

于 2013-05-07T16:50:58.043 回答