MATLAB Programming
MATLAB Programming MATLAB Programming
9 Classes and Objects case 'date' a.date = val; case 'currentValue' a.currentValue = val; otherwise error('Invalid field name') end end The subsasgn method enables you to assign values to the asset object data structure using two techniques. For example, suppose you have a child stock object s. (If you want to run this statement, you first need to create a stock constructor method.) s = stock('XYZ',100,25); Within stock class methods, you could change the descriptor field with either of the following statements s.asset(1) = 'ABC'; or s.asset.descriptor = 'ABC'; See the “The Stock subsasgn Method” on page 9-55 for an example of how the child subsasgn method calls the parent subsasgn method. The Asset display Method The asset display method is designed to be called from child-class display methods. Its purpose is to display the data it stores for the child object. The method simply formats the data for display in a way that is consistent with the formatting of the child’s display method. function display(a) % DISPLAY(a) Display an asset object stg = sprintf(... 'Descriptor: %s\nDate: %s\nType: %s\nCurrent Value:%9.2f',... a.descriptor,a.date,a.type,a.currentValue); disp(stg) 9-48
Example — Assets and Asset Subclasses The stock class display method can now call this method to display the data stored in the parent class. This approach isolates the stock display method from changes to the asset class. See “The Stock display Method” on page 9-57 for an example of how this method is called. TheAssetfieldcountMethod The asset fieldcount method returns the number of fields in the asset object data structure. fieldcount enables asset child methods to determine the number of fields in the asset object during execution, rather than requiring thechildmethodstohaveknowledgeoftheassetclass. Thisallowsyouto make changes to the number of fields in the asset class data structure without having to change child-class methods. function numFields = fieldcount(assetObj) % Determines the number of fields in an asset object % Used by asset child class methods numFields = length(fieldnames(assetObj)); The struct function converts an object to its equivalent data structure, enabling access to the structure’s contents. Designing the Stock Class A stock object is designed to represent one particular asset in a person’s investment portfolio. This object contains two properties of its own and inherits three properties from its parent asset object. Stock properties: • NumberShares — The number of shares for the particular stock object. • SharePrice — The value of each share. Asset properties: • Descriptor — The identifier of the particular asset (e.g., stock name, savings account number, etc.). • Date — The date the object was created (calculated by the date command). • CurrentValue — The current value of the asset. 9-49
- Page 658 and 659: 8 Error Handling end function f2(x)
- Page 660 and 661: 8 Error Handling Debugging Errors a
- Page 662 and 663: 9 Classes and Objects Classes and O
- Page 664 and 665: 9 Classes and Objects The diagram s
- Page 666 and 667: 9 Classes and Objects functions do
- Page 668 and 669: 9 Classes and Objects visible only
- Page 670 and 671: 9 Classes and Objects Class Method
- Page 672 and 673: 9 Classes and Objects Examples of C
- Page 674 and 675: 9 Classes and Objects not support p
- Page 676 and 677: 9 Classes and Objects overloaded me
- Page 678 and 679: 9 Classes and Objects A(1,2).name(3
- Page 680 and 681: 9 Classes and Objects Object Indexi
- Page 682 and 683: 9 Classes and Objects Converter Met
- Page 684 and 685: 9 Classes and Objects Operation M-F
- Page 686 and 687: 9 Classes and Objects Example — A
- Page 688 and 689: 9 Classes and Objects to the .c fie
- Page 690 and 691: 9 Classes and Objects end if d >= 2
- Page 692 and 693: 9 Classes and Objects % SUBSREF swi
- Page 694 and 695: 9 Classes and Objects q = polynom(q
- Page 696 and 697: 9 Classes and Objects % POLYNOM/DIF
- Page 698 and 699: 9 Classes and Objects Building on O
- Page 700 and 701: 9 Classes and Objects Multiple Inhe
- Page 702 and 703: 9 Classes and Objects Inheritance M
- Page 704 and 705: 9 Classes and Objects a.currentValu
- Page 706 and 707: 9 Classes and Objects error('Asset
- Page 710 and 711: 9 Classes and Objects Note that the
- Page 712 and 713: 9 Classes and Objects For example,
- Page 714 and 715: 9 Classes and Objects The Stock sub
- Page 716 and 717: 9 Classes and Objects case '.' swit
- Page 718 and 719: 9 Classes and Objects Example — T
- Page 720 and 721: 9 Classes and Objects if isa(name,'
- Page 722 and 723: 9 Classes and Objects end if saving
- Page 724 and 725: 9 Classes and Objects Saving and Lo
- Page 726 and 727: 9 Classes and Objects • subsref
- Page 728 and 729: 9 Classes and Objects function n =
- Page 730 and 731: 9 Classes and Objects Object Preced
- Page 732 and 733: 9 Classes and Objects How MATLAB De
- Page 734 and 735: 9 Classes and Objects Private funct
- Page 736 and 737: 9 Classes and Objects 9-76
- Page 738 and 739: 10 Scheduling Program Execution wit
- Page 740 and 741: 10 Scheduling Program Execution wit
- Page 742 and 743: 10 Scheduling Program Execution wit
- Page 744 and 745: 10 Scheduling Program Execution wit
- Page 746 and 747: 10 Scheduling Program Execution wit
- Page 748 and 749: 10 Scheduling Program Execution wit
- Page 750 and 751: 10 Scheduling Program Execution wit
- Page 752 and 753: 10 Scheduling Program Execution wit
- Page 754 and 755: 10 Scheduling Program Execution wit
- Page 756 and 757: 10 Scheduling Program Execution wit
9 Classes and Objects<br />
case 'date'<br />
a.date = val;<br />
case 'currentValue'<br />
a.currentValue = val;<br />
otherwise<br />
error('Invalid field name')<br />
end<br />
end<br />
The subsasgn method enables you to assign values to the asset object data<br />
structure using two techniques. For example, suppose you have a child stock<br />
object s. (If you want to run this statement, you first need to create a stock<br />
constructor method.)<br />
s = stock('XYZ',100,25);<br />
Within stock class methods, you could change the descriptor field with<br />
either of the following statements<br />
s.asset(1) = 'ABC';<br />
or<br />
s.asset.descriptor = 'ABC';<br />
See the “The Stock subsasgn Method” on page 9-55 for an example of how the<br />
child subsasgn method calls the parent subsasgn method.<br />
The Asset display Method<br />
The asset display method is designed to be called from child-class display<br />
methods. Its purpose is to display the data it stores for the child object. The<br />
method simply formats the data for display in a way that is consistent with<br />
the formatting of the child’s display method.<br />
function display(a)<br />
% DISPLAY(a) Display an asset object<br />
stg = sprintf(...<br />
'Descriptor: %s\nDate: %s\nType: %s\nCurrent Value:%9.2f',...<br />
a.descriptor,a.date,a.type,a.currentValue);<br />
disp(stg)<br />
9-48