我们说我有两个课foo和bar:
classdef foo
properties
prop1;
prop2;
end
end
classdef bar < foo
properties
prop3;
prop4;
end
end
如果我初始化bar,我会得到以下内容:
b =
bar with properties:
prop3: []
prop4: []
prop1: []
prop2: []
有没有办法改变属性的顺序,使它们按顺序出现(prop1,prop2,prop3,prop4)?
答案 0 :(得分:0)
感谢Amro我能够提出一个有效的解决方案。它可能不是最干净的方法,但似乎有效。我上了课:
classdef CustomPropertyOrder
methods
function propList = properties(obj)
% Get info about the class
classInfo = metaclass(obj);
% Skip if it's the CustomPropertyOrder class
if ~strcmp(classInfo.Name, 'CustomPropertyOrder')
% Get list of superclasses
superclassList = [classInfo.SuperclassList];
% Get list of all properties
allPropertyList = [classInfo.PropertyList];
% Get list of the classes which define each property
defPropertyList = [allPropertyList.DefiningClass];
% Get the names of the properties
propertyNameList = {allPropertyList.Name};
% Get the defining classes of the properties
propertyClassList = {defPropertyList.Name};
% Get just the properties defined by the current class
myProperties = propertyNameList(strcmp(propertyClassList, classInfo.Name))';
% Loop through all the superclasses
for idx = 1:length(superclassList)
% Create an instance of the superclass
f = str2func(superclassList(idx).Name);
t = f();
% Get the properties of the superclass
supProperties = properties(t);
end
% Concat the property lists, putting the superclass properties
% first
propList = [supProperties; myProperties];
else
% Return an empty property list
propList = [];
end
end
end
端
每当我需要按照我需要的顺序显示属性时,我就会从这个类中派生出来。