Download presentation
Presentation is loading. Please wait.
Published byConrad Cobb Modified over 9 years ago
1
Get/Set Methods UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. http://jagger.me.berkeley.edu/~pack/e77http://creativecommons.org/licenses/by-sa/2.0/
2
Access methods: get and set The public properties of the object are –Retrieved using get –Modified using set, which can control changes that are allowed Properties have names to identify them, so a natural syntax is PropertyValue = get(Object,PropertyName) set(Object,PropertyName,PropertyValue) For the e77poly class, let’s make Symbol a public property –Its value is the single character symbol used to represent the indeterminate variable of the polynomial –Since the user will be able to control its value, we won’t allow binary operations (like + and * ) unless the symbols match.
3
Writing get is pretty easy function value = get(p,PropName) switch PropName case 'Symbol' value = p.symbol; otherwise error('Unknown property'); end The convention (we’ll see next week) in Matlab for value = get(Object) is to return a struct, whose fieldnames are the public properties. Easy to add this in, using nargin. Limited access
4
Writing set is a little harder Why won’t this work? function set(p,PropName,value) switch PropName case 'Symbol‘ if isa(value,’char’) & isscalar(value) p.symbol = value; else error(’Invalid Symbol value’); end otherwise error('Unknown property'); end Controlling what is allowed
5
Writing set is a little harder Without some trickery, the command >> set(a,PropName,Value) will not work as intended. In other words, after executing >> initvalue = get(a,PropName); >> newvalue = some_new_value; >> set(a,PropName,newvalue) the result of >> isequal(get(a,PropName),newvalue) is false, and the result of >> isequal(get(a,PropName),initvalue) is true. Why? (week 2/3): variables are passed to functions by value, changing them in the function only changes the function’s copy.
6
Accessing other workspaces Two built-in Matlab commands to circumvent this by –Allowing assignment of variables in other workspaces –Within a function, giving access to the name of an input argument variable in the caller’s workspace Specifically, these two functions are assignin –copy a variable from the current workspace into a different workspace, with a new, given name. inputname –Access the name (in the caller’s workspace) of an input argument variable.
7
Example with assignin >> >> v1 = [1 -2 3]; >> W = EXAMP1(v1); >> function [B] = EXAMP1(A) assignin(’caller’,’NEWV’,2*A); B = 3*A; BASE WORKSPACE v1: [1 -2 3] NEWV: [2 -4 6] W: [3 -6 9] FUNCTION INSTANCE WS A: [1 -2 3] B: [3 -6 9] function [B] = EXAMP1(A) assignin(’caller’,’NEWV’,2*A); B = 3*A;
8
Example with inputname >> >> fred = [2 3]; >> joe = [5 5]; >> W = EXAMP2(fred,joe); >> function [C] = EXAMP2(A,B) D = inputname(1); E = inputname(2); C = A + B; BASE WORKSPACEFUNCTION INSTANCE WS D: ’fred’ E: ’joe’ C: [7 8] function [C] = EXAMP2(A,B) D = inputname(1); E = inputname(2) C = A + B; fred: [2 3] joe: [5 5] W: [7 8] A: [2 3] B: [5 5]
9
Example using both Look at the function incr.m function incr(a) assignin(’caller’,inputname(1),a+1); It increments, without an output argument! >> var = 7; >> incr(var); >> var var = 8 >>
10
Fixing set uses this idea Here is one working version of set function set(p,PropName,value) switch PropName case 'Symbol' p.symbol = value; % need error check otherwise error('Unknown property'); end assignin(’caller’,inputname(1),p); Could also pass back an argument, or give user the option to do either…
11
A version of set allowing for either… function pnew = set(p,PropName,value) switch PropName case 'Symbol' p.symbol = value; % need error check otherwise error('Unknown property'); end if nargout==0 assignin(’caller’,inputname(1),p); else pnew = p; end
12
Nearly finished: Methods for e77poly Constructor, e77poly converter and display methods –double, converts a polynomial to a row vector of coefficients –char, converts a polynomial to a formatted string –display, for expressions that result in an e77poly object Operator overloaded methods –plus(A,B) for A+B –minus(A,B) for A-B –uplus(A) for +A –uminus(A) for -A –mtimes(A,B) for A*B Overloaded methods –roots, polyval, polyder, polyint Access methods ( get, set ) @e77poly e77poly.m double.m char.m display.m plus.m minus.m uplus.m uminus.m mtimes.m roots.m polyval.m polyder.m polyint.m get.m set.m
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.