您可以將E
和adjL
都定義爲依賴項,並且具有一個source屬性,該屬性包含實際值以及描述其源的標誌,然後定義getter方法以決定是應返回實際源還是轉換需要:
classdef graph
properties (Access=private)
sourcetype
source
end
properties (Dependent)
E
adjL
end
methods
function obj = graph(varargin)
if nargin==1,
obj.sourcetype = 'edge';
obj.source = varargin{1};
elseif nargin==2 && strcmp(varargin{2}, 'adjL')
obj.sourcetype = 'adjL';
obj.source = varargin{1};
else
error('Invalid input arguments');
end
end
function ret = get.E(obj)
if strcmp(obj.sourcetype, 'edge')
ret = obj.source;
else
% convert source from adjL to E
end
end
function ret = get.adjL(obj)
if strcmp(obj.sourcetype, 'adjL')
ret = obj.source;
else
% convert source from E to adjL
end
end
end
end
對於遲到的回覆感到抱歉。爲了使「E」和「adjL」都有依賴性,請參閱Mohsen的回答。我也想過編寫getter和setter,它似乎是保持'最新'屬性的唯一方法,儘管兩者現在都存儲在對象中。但是這引出了另一個問題:如果'E'是正常屬性,我應該使用依賴屬性'adjL',還是我應該寫一個'obj.adjL'方法?你有什麼想法? – scmg