Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

CRC.Press A Guide to MATLAB Object Oriented Programming May.2007 Episode 2 Part 2 doc
Nội dung xem thử
Mô tả chi tiết
194 A Guide to MATLAB Object-Oriented Programming
15.1.1.2 cLineStyle’s fieldnames
Whereas ctor_ini defines the collection of private variables, fieldnames defines the collection of public variables. In this case, there are only three: Color, LineWeight, and LineHandle. The public variables and the values they hold come directly from the requirements. The code
to implement fieldnames for these public variables is shown in Code Listing 86.
4 this(1).mColorHsv = [2/3; 1; 1]; % [H S V]’ of border,
default is blue
5 this(1).mLineWidth = 1; % line weight: ‘normal’ == 1 ‘bold’
== 3
6 this(1).mLineHandle = []; % handle for shape’s line plot
7 superior = {};
8 inferior = {};
9 parents = {};
10 parent_list(parents{:});
Code Listing 86, Modular Code, cLineStyle’s fieldnames.m
1 function names = fieldnames(this, varargin)
2 names = {};
3
4 % first fill up names with parent public names
5 parent_name = parent_list; % get the parent name cellstr
6 for parent_name = parent_list'
7 names = [names; fieldnames([this.(parent_name{1})],
varargin{:})];
8 end
9
10 % returns the list of public member variable names
11 if nargin == 1
12 names = {'Color' 'LineWidth' 'LineHandle'}';
13 else
14 switch varargin{1}
15 case '-full'
16 names = {'Color % double array' ...
17 'LineWidth' % positive integer' ...
18 'LineHandle % plot handle'}';
19 case '-possible'
20 names = {'Color' {{'double array (3x1)'}} ...
21 'LineWidth' {{'positive integer'}} ...
22 'LineHandle' {{'plot handle'}}}';
23 Otherwise
24 error('Unsupported call to fieldnames');
25 end
26 end
C911X_C015.fm Page 194 Friday, March 30, 2007 11:39 AM
Constructing Simple Hierarchies with Composition 195
The parent-forwarding code in lines 4–8 is not necessary because parent_list returns an
empty cellstr. It is included because it is part of the standard template. When fieldnames
is called with one input argument, line 12 returns a cellstr populated with the three public
variable names. Lines 16–18 and 20–22 return additional information that depend respectively on
‘-full’ and ‘-possible’ flag values. In line 21, note the possible values for LineWeight
are ‘normal’ or ‘bold’.
15.1.1.3 cLineStyle’s get
The public variable section for cLineStyle’s get is shown in Code Listing 87. By now, the
code in this listing should look familiar. The value of found on line 2 is used to control entry
into subsequent concealed variable, parent-forwarding, and error code blocks. Inside the switch
beginning on line 3, there is a case for each public member variable. Lines 4–10 came directly
from the public member variable section of cShape’s previous implementation. The remaining
cases simply map one private variable into one public variable. This public variable section is just
about as easy as it gets. The remaining sections of cLineStyle’s get function use code from
the standard group-of-eight template.
Code Listing 87, Public Variable Implementation in cLineStyle’s get.m
1 % public-member-variable section
2 found = true; % otherwise-case will flip to false
3 switch index(1).subs
4 case 'Color'
5 if isempty(this)
6 varargout = {};
7 else
8 rgb = hsv2rgb([this.mColorHsv]')';
9 varargout = mat2cell(rgb, 3, ones(1, size(rgb,2)));
10 end
11 case 'LineWidth'
12 if isempty(this)
13 varargout = {};
14 else
15 varargout = {this.mLineWidth};
16 end
17 case 'LineHandle'
18 if isempty(this)
19 varargout = {};
20 else
21 varargout = {this.mLineHandle};
22 end
23 otherwise
24 found = false; % didn't find it in the public section
25 end
C911X_C015.fm Page 195 Friday, March 30, 2007 11:39 AM