【问题标题】:How do I inherit documentation from super classes in Matlab?如何从 Matlab 中的超类继承文档?
【发布时间】:2013-05-01 16:05:34
【问题描述】:

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

有没有办法做到这一点?

【问题讨论】:

  • 我认为不可能做到这一点;但是,您可以包含一个“另见”,它将自动链接到您的超类;只需将 See also CLASSA. 放在您的 ClassB 文档的末尾,然后 MATLAB 将完成剩下的工作。
  • 我不知道这个。这很聪明。

标签: matlab oop inheritance subclass superclass


【解决方案1】:

如果您以您描述的方式编写文档,我认为获得所需内容的唯一方法是重载 help 以进行定制。例如,重载的help 可以对其自身调用builtin('help'),然后对其超类调用builtin('help')

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

【讨论】:

  • 您有指向传统文档方式的链接吗?我遵循了outlined here的建议。
  • 查看@Amro 的更好答案。
【解决方案2】:

正如@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

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

您会得到格式精美的超链接。

还要注意您使用doc Derived 得到的结果(或者当您突出显示单词并按F1 时)。这在内部调用 help2html 将帮助转换为 HTML。例如,我们可以自己这样做:

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

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

【讨论】:

  • 事实上,我曾经发现doc/help2html 的这种行为是不可取的,尤其是当您的类继承自handle 类时。要关闭它,我必须编辑内部 MALTAB 函数:[toolboxdir('matlab') '/helptools/private/help2xml.m']:pastebin.com/AYMa2p5q
  • 如果您觉得这很有用 - 我通常不会修改内部函数,而是从我自己的类 HandleHiddenMethods 继承。这继承自 handle,并由单个 methods (Hidden=true) 块组成。这是handle 的所有方法的副本,重载后直接将调用传递给handle。所以HandleHiddenMethods 的行为与handle 相同,但不会在自动生成的文档中显示其方法。这适用于 isvalid 以外的所有方法,它是密封的,不能重载。
  • @SamRoberts:聪明,感谢分享这个技巧。跟踪这些修改并不容易,尤其是在每个新版本都升级 MATLAB 时。
猜你喜欢
  • 1970-01-01
  • 2017-08-24
  • 2019-12-22
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2012-09-10
相关资源
最近更新 更多