CRC.Press A Guide to MATLAB Object Oriented Programming May.2007 Episode 2 Part 4 ppsx

20 322 0
CRC.Press A Guide to MATLAB Object Oriented Programming May.2007 Episode 2 Part 4 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

234 A Guide to MATLAB Object-Oriented Programming 17.2.1.4 Concealed Variables … Dialog Clicking the Concealed Variables … button brings up the dialog shown in Figure 17.7. The dialog shown in the figure contains an entry for one variable. Like most of the tool’s dialogs, the concealed variable dialog is organized into three sections: input fields, action buttons, and a display. In Figure 17.7, the input fields contain values because a variable name in the display is selected. Selecting a blank line will empty all values in the input fields. Even the name field will be empty because there is no suggested naming convention. Any legal variable name except mDisplayFunc can be entered in the field. The variable name mDisplayFunc is reserved for Class Wizard’s use, and Class Wizard will include it in class code when necessary. The five data-entry fields in the concealed variable dialog are described by the following: Concealed Variable Name: holds the name of a concealed member variable. Concealed variables are added one at a time. Type: holds a string that describes the variable’s type. In response to get(obj) or set(obj), concealed variables are not displayed; however, the type string is included in the header comments of several files, get and set for example. Accessor Expression: holds the expression used for accessing the variable. The contents of this field are limited to two special cases: the exact name of a private member variable or the keyword %helper. When the name of a private member variable is used, direct-link accessor code is included in get.m. This option also allows clients to specify additional indices beyond the first dot-reference. The %helper keyword causes FIGURE 17.7 Class Wizard, Concealed Variables … dialog. C911X_C017.fm Page 234 Friday, March 2, 2007 8:50 AM Class Wizard 235 helper-function syntax (non-direct-link) to be used inside get.m. The %helper keyword also triggers the generation of a helper-function stub. The stub contains a complete header along with some initial code and comments. Accessing a concealed variable with a default helper will not cause an error, but the return value will be empty until the helper is customized. Mutator Expression: holds the expression used for assigning values into the variable. Like the accessor, the contents of this field are limited to two special cases: the exact name of a private member variable or the keyword %helper. When the name of a private member variable is used, direct-link mutator code is included in set.m. This option also allows clients to specify additional indices beyond the first dot-reference. The %helper keyword causes helper-function syntax to be used inside set.m. The %helper keyword also triggers the generation of a helper-function stub. The stub contains a complete header along with some initial code and comments. Mutating a concealed variable with a default helper will not cause an error, but values in the object will not be modified until the helper is customized. Comment: holds a text description of the variable. The text description is important because it will show up along with the variable name and variable type in various header comments, where it will serve as a reference to other developers. In this case, the display list box does not provide a what-you-see-is-what-you-get (WYSIWYG) format. WYSIWYG is not possible because each name links to multiple files in the group-of-eight and each file uses the name differently. Instead of WYSIWYG, the display box shows individual fields separated by double colons. This achieves the goal of providing a good one-line overview without the complications involved in writing the exact syntax. The three buttons, Save Change, Delete, and Done, are the same three buttons used in the parent-class dialog. Refer to §17.2.1.2 for a description of their behavior. When you finish modifying the object’s concealed data, click the Done button. This will save your changes and return you to the main dialog. Concealed-variable names are shown in the list box adjacent to the Concealed Variables … button. The names provide a quick summary, and in the main dialog view they are not active. You have to open the concealed variable dialog to make changes or see more detail. 17.2.1.5 Public Variables … Dialog Clicking the Public Variables … button brings up the dialog shown in Figure 17.8. The dialog shown in the figure contains an entry for one variable. Like most of the tool’s dialogs, the public variable dialog is organized into three sections: input fields, action buttons, and a display. In Figure 17.8, the input fields contain values because a variable name in the display is selected. Selecting a blank line will empty all values in the input fields. Even the name field will be empty because there is no suggested naming convention. Any legal variable name can be entered in the name field. The fields in this dialog are identical to those for concealed variables because in reality the only difference is the simplified syntax used to access public variables. Public variables can be accessed or mutated using the dot-reference operator via subsref and subsasgn, while access to concealed-variables is limited to get and set. In the code, public variables are written into the public section of get and set while concealed variables are written into the concealed section. Beyond that, there is little difference between public and concealed. The five data-entry fields in the public variable dialog are as follows: C911X_C017.fm Page 235 Friday, March 2, 2007 8:50 AM 236 A Guide to MATLAB Object-Oriented Programming Public Variable Name: holds the name of a public member variable. Public variables are added one at a time. Type: holds a string that describes the variable’s type. This string is displayed along with the variable name when get(obj) or set(obj) is used to display member variable hints. The type string also shows up in the header comments of various files, get and set for example. Accessor Expression: holds the expression used for accessing the variable. The contents of this field are limited to two special cases: the exact name of a private member variable or the keyword %helper. When the name of a private member variable is used, direct-link accessor code is used inside get.m. This option also allows clients to specify additional indices beyond the first dot-reference. The %helper keyword causes helper- function syntax (non-direct-link) to be used inside get.m. The %helper keyword also triggers the generation of a helper-function stub. The stub contains a complete header along with some initial code and comments. Accessing a public variable with a default helper will not cause an error, but the return value will be empty until the helper is customized. Mutator Expression: holds the expression used for assigning values into the variable. Like the accessor, the contents of this field are limited to two special cases: the exact name of a private member variable or the keyword %helper. When the name of a private member variable is used, direct-link mutator code is used inside set.m. This option also allows clients to specify additional indices beyond the first dot-reference. The %helper keyword causes helper-function syntax to be used inside set.m. The %helper keyword also triggers the generation of a helper-function stub. The stub contains a complete header FIGURE 17.8 Class Wizard, Public Variables … dialog. C911X_C017.fm Page 236 Friday, March 2, 2007 8:50 AM Class Wizard 237 along with some initial code and comments. Mutating a public variable with a default helper will not cause an error, but values in the object will not be modified until the helper is customized. Comment: holds a text description of the variable. The text description is important because it will show up along with the variable name and variable type in various header comments, where it will serve as a reference to other developers. In this case, the display list box does not provide a what-you-see-is-what-you-get (WYSIWYG) format. WYSIWYG is not possible because each name links to multiple files in the group of eight and each file uses the name differently. Instead of WYSIWYG, the display box shows individual fields separated by double colons. This achieves the goal of providing a good one-line overview without the complications involved in writing the exact syntax. The three buttons, Save Change, Delete, and Done, are the same three buttons used in the parent-class dialog. Refer to §17.2.1.2 for a description of their behavior. When you finish modifying the object’s public data, click the Done button. This will save your changes and return you to the main dialog. Public variable names are shown in the list box adjacent to the Public Variables … button. The names provide a quick summary, and in the main dialog view they are not active. You have to open the public variable dialog to make changes or see more detail. 17.2.1.6 Constructors … Dialog Clicking the Constructors … button brings up the dialog shown in Figure 17.9. The dialog shown in the figure contains an entry for one constructor in addition to the default. Like most of the tool’s dialogs, the constructor dialog is organized into three sections: input fields, action buttons, and a display. In Figure 17.9, the lone input field contains a value because a constructor name in the display is selected. Selecting a blank line will empty this field. FIGURE 17.9 Class Wizard, Constructors … dialog. C911X_C017.fm Page 237 Friday, March 2, 2007 8:50 AM 238 A Guide to MATLAB Object-Oriented Programming In the selected case, the constructor-helper function will be named ctor_1 because there is one input argument. This is consistent with the constructor-helper naming convention previously discussed. Inputs are specified using the comma-separated list, and Class Wizard generates a stub for the helper. The single data-entry field in the constructor dialog is described by the following: Input Argument List: holds a comma-separated list of input arguments for each constructor-helper function. The variable name this should never be included in the input argument list. When files are generated, helper-function names are set according to the number of variables in each comma-separated list. The function prototype for each helper includes this along with the names in the comma-separated list. Functional stubs for each constructor helper are written into the class’ private directory. The default stub does not know how to use the input arguments, so you must tailor each helper according to the specific application. Variables from each comma-separated list are added to the data dictionary. Comments can be associated with each dictionary name using the main dialog’s Data menu item. The display list box provides a WYSIWYG format for the function definition of each helper. In this case displayed in Figure 17.9, the function name is ctor_1 because the constructor is meant for the case when an object is constructed from one input. The function prototype for ctor_1 actually contains two inputs: this and the variable from the Input Argument List field. The object itself is passed in and out of the constructor because the whole purpose is to populate the object with values other than default. The constructor helper can modify the value’s existing private variables but cannot add new fields. This helps protect the integrity of the objects. The three buttons, Save Change, Delete, and Done, are the same three buttons used in the parent-class dialog. Refer to §17.2.1.2 for a description of their behavior. When you finish modifying the object’s public data, click the Done button. This will save your changes and return you to the main dialog. Helper names are shown in the list box adjacent to the Constructors … button. The names provide a quick summary, and in the main dialog view they are not active. You have to open the constructor dialog to make changes or see more detail. 17.2.1.7 More … Dialog Clicking the More … button brings up the dialog shown in Figure 17.10. This dialog provides access to a group of data-entry dialogs for additional class features. Most of these features involve Part 3 topics; however, Private Functions … and Public Functions … are immediately useful. With these buttons, you can enter data for class-specific public and private functions. Dialogs are activated by respectively clicking the buttons Public Functions … and Private Functions …. Unlike functions in the group of eight, Class Wizard does not know how to configure code for general public and private functions. Instead, Class Wizard resorts to the same strategy used for helper functions and generates a function stub. The function stub will contain a consistently formatted function call and a header that includes detailed function comments. These comments draw from the data dictionary to include information for input–output variables. As a result, the generated member functions usually have more detailed comments compared to those that are not automatically generated. The stub also includes enough code to allow the function to execute, albeit at a greatly reduced level of function. The other buttons in the dialog represent class features that are less common but no less useful under the right circumstances. At first, the number of buttons is daunting; however, the layouts of the underlying dialogs are very similar if not identical to those we have already discussed. Entering data follows the same procedure. The data end up in slightly different locations. You already know the most important locations, and the next section discusses a few more elements. C911X_C017.fm Page 238 Friday, March 2, 2007 8:50 AM Class Wizard 239 17.2.1.8 Static Variables … Dialog Clicking the Static Variables … button brings up the dialog shown in Figure 17.11. The dialog shown in the figure contains an entry for one variable. Like most of the tool’s dialogs, the static variable dialog is organized into three sections: input fields, action buttons, and a display. In Figure 17.11 the input fields contain values because a variable name in the display is selected. Selecting a blank line will insert an m in Static Variable Name, and the other fields will be blank. A leading m shows up by default because a static variable is a special kind of private variable. The difference between static and private is scope. With a private variable, every object gets its own copy; but with a static variable, every object of the class shares one copy. If one object sets the value, that value shows up in all objects of the class. When static variables are defined, the private helper function static.m is used to manage them. The interface to static.m is very similar to the interface defined for accessor–mutator helpers. The details are described in Chapter 20. The three data-entry fields in the static variable dialog are described by the following: Static Variable Name: holds the name of a static member variable. Static variables are added one at a time. Initial Value: holds the default value that will be assigned to the variable by the constructor. The field value is entered using the exact syntax required in the assignment. The display list box formats the input exactly as it will be written into ctor_ini.m. What you see in the display list box is exactly what you get in ctor_ini.m. Virtually any legal syntax can be used. Comment: holds a text description of the variable. The text description is important because it will show up in the header of ctor_ini.m, where it will serve as a future reference for developers. The three buttons, Save Change, Delete, and Done, are the same three buttons used in the parent-class dialog. Refer to §17.2.1.2 for a description of their behavior. When you finish modifying the object’s parent data, click the Done button. This will save your changes and return you to the more dialog. Static member variable names are shown in the list box adjacent to the Static Variables … button. The names provide a quick summary, and in the more dialog FIGURE 17.10 Class Wizard, More … dialog. C911X_C017.fm Page 239 Friday, March 2, 2007 8:50 AM 240 A Guide to MATLAB Object-Oriented Programming view they are not active. You have to open the static variable dialog to make changes or see more detail. 17.2.1.9 Private Functions … Dialog Clicking the Private Functions … button brings up the dialog shown in Figure 17.12. The dialog shown in the figure contains an entry for one function. Like most of the tool’s dialogs, the private-function dialog is organized into three sections: input fields, action buttons, and a display. In Figure 17.12, the input fields contain values because a function name in the display is selected. Selecting a blank line will empty all values in the input fields. Even the name field will be empty because there is no suggested naming convention. Any legal function name can be entered in the name field. In reality, constructor helpers, accessor–mutator helpers, and parent_list.m are private functions; however, these are part of the group-of-eight interface and are managed separately from class-specific private functions. Only class-specific private functions show up in this list. Private functions, like most functions, can be described by a function name, a list of input arguments, a list of output arguments, and a comment. The data-entry fields for these four function elements are described by the following: Function Name: holds the desired name for the function. Any valid function name can be used. Input Argument List: holds a comma-separated list of input arguments for the function. If a copy of the object must be passed, this must be included in the argument list. By convention, when it exists, this should usually be the first argument in the list. In addition, notice there is no way to enter comments for individual arguments. Instead, variable names are added to the data dictionary using the Data menu on the main screen. FIGURE 17.11 Class Wizard, Static Variables … dialog. C911X_C017.fm Page 240 Friday, March 2, 2007 8:50 AM Class Wizard 241 Output Argument List: holds a comma-separated list of output arguments for the function. If a copy of the object is passed, this must be included in the argument list. By convention, when it exists, this should usually be the first argument in the argument list. As with variables in the input argument list, output variable names are added to the data dictionary. Comment: holds a text description of the function. The text description will be added to the header comments when the function is generated. Clicking the Save Change button commits the changes and displays the function prototype in WYSIWYG format. During file generation, Class Wizard writes the same function prototype into the private function and follows the prototype with header comments. Of course, Class Wizard doesn’t know how to write the real function body, but it can write a body that runs without error. The body of the private function must be manually modified to include the desired class-specific functionality. The three buttons, Save Change, Delete, and Done, are the same three buttons used in the parent-class dialog. Refer to §17.2.1.2 for a description of their behavior. When you finish modifying the object’s private-function data, click the Done button. This will save your changes and return you to the more dialog. Private-function names are shown in the list box adjacent to the Private Functions … button. The names provide a quick summary, and in the more dialog view they are not active. You have to open the private-function dialog to make changes or see more detail. FIGURE 17.12 Class Wizard, Private Function … dialog. C911X_C017.fm Page 241 Friday, March 2, 2007 8:50 AM 242 A Guide to MATLAB Object-Oriented Programming 17.2.1.10 Public Functions … Dialog Clicking the Public Functions … button brings up the dialog shown in Figure 17.13. The dialog shown in the figure contains an entry for one function. Like most of the tool’s dialogs, the public-function dialog is organized into three sections: input fields, action buttons, and a display. In Figure 17.13, the input fields contain values because a function name in the display is selected. Selecting a blank line will empty all values in the input fields. Even the name field will be empty because there is no suggested naming convention. Any legal function name can be entered in the name field. In reality, standard group-of-eight functions are public functions; however, group-of-eight functions are managed separately from class-specific public functions. Only class-specific public functions show up in this list. Public functions, like most functions, can be described by a function name, a list of input arguments, a list of output arguments, and a comment. The data-entry fields for these four function elements are described by the following: Function Name: holds the desired name for the function. Any valid function name can be used. Input Argument List: holds a comma-separated list of input arguments for the function. If a copy of the object must be passed, this must be included in the argument FIGURE 17.13 Class Wizard, Public Function … dialog. C911X_C017.fm Page 242 Friday, March 2, 2007 8:50 AM Class Wizard 243 list. By convention, when it exists, this should usually be the first argument in the list. In addition, notice there is no way to enter comments for individual arguments. Instead, variable names are added to the data dictionary using the Data menu on the main screen. Output Argument List: holds a comma-separated list of output arguments for the function. If a copy of the object is passed, this must be included in the argument list. By convention, when it exists, this should usually be the first argument in the argument list. As with variables in the input argument list, output variable names are added to the data dictionary. Comment: holds a text description of the function. The text description will be added to the header comments when the function is generated. Clicking the Save Change button commits the changes and displays the function prototype in WYSIWYG format. During file generation, Class Wizard writes the same function prototype into the private function and follows the prototype with header comments. Of course, Class Wizard doesn’t know how to write the real function body, but it can write a body that runs without error. The body of the private function must be manually modified to include the desired class-specific functionality. The three buttons, Save Change, Delete, and Done, are the same three buttons used in the parent-class dialog. Refer to §17.2.1.2 for a description of their behavior. When you finish modifying the object’s public-function data, click the Done button. This will save your changes and return you to the more dialog. Public-function names are shown in the list box adjacent to the Public Functions … button. The names provide a quick summary, and in the more dialog view they are not active. You have to open the public-function dialog to make changes or see more detail. 17.2.1.11 File Menu The main Class Wizard dialog has two menu items: File and Data. The File menu allows you to create a New class definition, Open … an existing definition, Save the current definition, or Save as … to save the current definition using a different file name. These menu items behave the same as any application that opens and saves files. Their behaviors are described as follows: File::New: Select this item to begin a new class definition. The hot-key sequence is ctrl-N. All fields are reset to default initial values, usually empty. Currently, using File::New does not check for changes to the currently loaded values before loading a new file. You will not be warned to save your changes. File::Open : Selecting this item allows you to open an existing file using the standard fi le-open dialog box. The hot-key combination is ctrl-O. This familiar dialog is shown in Figure 17.14. The file format for definition files is .mat. This format is convenient because it allows you to load the definition file directly in MATLAB and tweak the data in ways not available through Class Wizard. File::Save: Selecting this item saves the current class definition file to the current file name. The hot-key sequence is ctrl-S. If there is no current filename, you will be prompted to enter one. File::Save As : Selecting this item opens a standard dialog box for specifying the file name during a save. The hot-key sequence is ctrl-A. The familiar dialog is shown in Figure 17.15. The definition file can be stored anywhere; however, the best location is in the class directory. When class functions are generated, the definition file is automatically saved in the same directory as the functions. C911X_C017.fm Page 243 Friday, March 2, 2007 8:50 AM [...]...C911X_C017.fm Page 24 4 Friday, March 2, 20 07 8:50 AM 24 4 A Guide to MATLAB Object- Oriented Programming FIGURE 17. 14 Class Wizard, standard File::Open … dialog FIGURE 17.15 Class Wizard, standard File::Save As … dialog 17 .2. 1. 12 Data Menu In most class definitions, the same variable names are used in several functions Rather than commenting these variables in several places, a data dictionary approach collects... variable cannot be changed This also means that new names can’t be added to the definition via the dictionary Names can only be added by defining a new name in a function call Data in the other fields can be modified to reflect the appropriate descriptions The three data fields in the data dictionary variable dialog are described by the following: C911X_C017.fm Page 24 5 Friday, March 2, 20 07 8:50 AM Class... into the display and into ctor_ini.m Virtually any value that can be written on the right-hand side of an assignment can be used as an initial value For example, a function call generates the initial value for mColorHsv Click Done to commit the changes and return to the main dialog When Class Wizard generates /@cLineStyle/private/ctor_ini, the private variable names and their default values are included... handle of the line plot C911X_C018.fm Page 25 3 Friday, March 2, 20 07 9:06 AM Class Wizard Versions of the Shape Hierarchy 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 25 3 % % Author Info % Replace with your company's name % Replace with your name % Replace with your email address % Replace with your phone number % Replace with the author notes that you would like to. .. file, add the new variables, and rebuild the files The class organization and default wizard settings make this possible 17.3 SUMMARY For building a robust MATLAB class, Class Wizard has no equal The various dialog screens are organized along the same divisions used to describe an object- oriented design This makes data entry much less of a chore Once the design data are entered, Class Wizard builds a full... chapter includes a copy of the as-generated files in a directory separate from the full solution 18.1 CLINESTYLE CLASS WIZARD DEFINITION DATA Begin by running the graphical interface for Class Wizard To do this, the directory /utils/wizard_gui must be on or added to the MATLAB path You can add the path in one of three ways: cd into the directory, use an addpath command, or use MATLAB s File::Set Path... selected directory in mat-file format The group-of-eight files are completely functional, and C911X_C017.fm Page 24 6 Friday, March 2, 20 07 8:50 AM 24 6 A Guide to MATLAB Object- Oriented Programming FIGURE 17.17 Class Wizard, Build Class Files dialog class-specific member functions exist as simple function stubs If new public or private member variables need to be added, it is a simple process to reload the definition... important because the hierarchy exercises most of the available options The primary activity in this chapter involves entering data into the various Class Wizard dialog screens As data are entered, the lower-list box in each dialog shows a line-by-line summary of the data To assist you in data entry, a screen shot of each completed dialog is included That way, all variables and functions are provided as you... close similarity to the standard idioms C911X_C017.fm Page 24 8 Friday, March 2, 20 07 8:50 AM C911X_C018.fm Page 24 9 Friday, March 2, 20 07 9:06 AM 18 Class Wizard Versions of the Shape Hierarchy In Chapter 17, we covered Class Wizard’s various input dialogs along with their general operation In this chapter, we demonstrate the complete Class Wizard process of developing a collection of classes To do this,... The names and comments are included in the header If you return to the main screen and click Build Class Files, Class Wizard will generate the file @cLineStyle/private/ctor_ini.m shown in Code Listing 105 Other files will also be generated, but ctor_ini is the current focus C911X_C018.fm Page 25 2 Friday, March 2, 20 07 9:06 AM 25 2 A Guide to MATLAB Object- Oriented Programming FIGURE 18.3 Class Wizard, . Page 24 3 Friday, March 2, 20 07 8:50 AM 24 4 A Guide to MATLAB Object- Oriented Programming 17 .2. 1. 12 Data Menu In most class definitions, the same variable names are used in several functions. Rather. Page 23 9 Friday, March 2, 20 07 8:50 AM 24 0 A Guide to MATLAB Object- Oriented Programming view they are not active. You have to open the static variable dialog to make changes or see more detail. 17 .2. 1.9. completely functional, and FIGURE 17.16 Class Wizard, Data File::Dictionary … dialog. C911X_C017.fm Page 24 5 Friday, March 2, 20 07 8:50 AM 24 6 A Guide to MATLAB Object- Oriented Programming class-specific

Ngày đăng: 05/08/2014, 21:21

Tài liệu cùng người dùng

Tài liệu liên quan