Is is possible to control the order of inherited class properties?

조회 수: 40 (최근 30일)
Harry Dymond
Harry Dymond 2020년 6월 19일
편집: Harry Dymond 2020년 6월 19일
Consider two simple classes, where one inherits from the other:
First class:
classdef Example_Class_1 < handle
properties
Property1
Property2
end
end
Second class:
classdef Example_Class_2 < Example_Class_1
properties
Property3
end
end
Now, if I create an Example_Class_2 object, its properties are listed as follows:
Example_Class_2
ans =
Example_Class_2 with properties:
Property3: []
Property1: []
Property2: []
Is there some alternative syntax I can use when defining Example_Class_2, such that Property1 and Property 2 (the inherited properties) appear before Property3 when an Example_Class_2 object is created?

채택된 답변

Harry Dymond
Harry Dymond 2020년 6월 19일
Following the advice of William Warriner, here is what can be done. Change Example_Class_2 as follows:
classdef Example_Class_2 < Example_Class_1 & matlab.mixin.CustomDisplay
properties
Property3
end
methods (Hidden)
function value = properties( obj )
propList = sort( builtin("properties", obj) );
if nargout == 0
disp(propList);
else
value = propList;
end
end
function value = fieldnames( obj )
value = sort( builtin( "fieldnames", obj ) );
end
end
methods (Access = protected)
function group = getPropertyGroups( obj )
props = properties( obj );
group = matlab.mixin.util.PropertyGroup( props );
end
end
end
Now, properties are listed in alphabetical order, both when displayed on the command line, and when returned as a cell array (with e.g. propList = properties(Example_Class_2))
Of course, you don't have to use sort() to re-order the property list in the properties method. In my case, I needed a different ordering and just used
propList = builtin("properties", obj);
propList = propList([<my custom indices order>]);

추가 답변 (2개)

Steven Lord
Steven Lord 2020년 6월 19일
Have your class inherit from matlab.mixin.CustomDisplay and override the getPropertyGroups method. See the "Customize Property Display" section on this documentation page for an example.
  댓글 수: 1
Harry Dymond
Harry Dymond 2020년 6월 19일
Hi Steven, thanks for the reply. Your answer appeared whilst I was working on mine!

댓글을 달려면 로그인하십시오.


Ashwin
Ashwin 2020년 6월 19일
Although there isn’t any explicit way by which the order of printing of the properties can be changed, you can attempt to overload the properties() function in order to print the properties of a required class in the order you require it. The below question does the same for dynamic properties, it could be of reference.
However, the process does seem rather tedious to reproduce.
  댓글 수: 1
Harry Dymond
Harry Dymond 2020년 6월 19일
편집: Harry Dymond 2020년 6월 19일
Thanks for the speedy reply! Actually, the answer on that thread from William Warriner is very simple to implement - see my accepted answer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Third-Party Cluster Configuration에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by