如何从Matlab中的超类继承文档?
我有一个超类,我已经做了大量的文档。有一些子类继承自这个超类,如果可能的话,我想重用超类的文档。例如,使用Super class如何从Matlab中的超类继承文档?,matlab,oop,inheritance,subclass,superclass,Matlab,Oop,Inheritance,Subclass,Superclass,我有一个超类,我已经做了大量的文档。有一些子类继承自这个超类,如果可能的话,我想重用超类的文档。例如,使用Super classClassA: classdef ClassA %CLASSA Super Class for all others classes % % CLASSA Properties: % Prop1 It's the first property % Prop2 It's the second
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
可以在自身上调用builtin('help')
,然后在其超类上调用builtin('help')
然而,你没有用标准的方式记录事情;通常,您会在属性本身的正上方使用注释记录属性,并在方法的函数签名的正下方使用注释记录方法。如果您这样做了,那么您将自动显示所有继承方法的帮助。如中所述,存在一系列记录属性和方法的方法
例如:
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
及
请注意,将显示继承的属性/方法。我认为这是不可能的;但是,您可以包含一个“see也”,它将自动链接到您的超级类;只需在
ClassB
文档末尾添加另请参见CLASSA.
,其余部分由MATLAB完成。我不知道这一点。这很聪明。你有传统的文档记录方式的链接吗?我遵循了这些建议。请看@Amro的更好答案。事实上,我曾经发现doc
/help2html
的这种行为是不可取的,尤其是当您的类继承自handle
类时。要关闭它,我必须编辑内部MALTAB函数:[toolboxdir('matlab')/helptools/private/help2xml.m']
:如果您觉得这很有用,而不是修改内部函数,我通常从自己的类继承HandleHiddenMethods
。它继承自句柄
,由单个方法(Hidden=true)
块组成。这里是handle
所有方法的副本,重载后只需将调用直接传递到handle
。因此HandleHiddenMethods
的行为与handle
相同,但不会在自动生成的文档中显示其方法。这适用于除isvalid
之外的所有方法,该方法是密封的,不能重载。@SamRoberts:聪明,谢谢分享这个技巧。跟踪这些修改并不容易,尤其是在每次新版本升级MATLAB时。
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
>> web(['text://' help2html('Derived')], '-noaddressbox', '-new')