1 2 3 4 5 6 7 8 9 10 11 12 13 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Manipulating Classes</title>
<meta name="description" content="GNU Octave: Manipulating Classes">
<meta name="keywords" content="GNU Octave: Manipulating Classes">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Object-Oriented-Programming.html#Object-Oriented-Programming" rel="up" title="Object Oriented Programming">
<link href="Indexing-Objects.html#Indexing-Objects" rel="next" title="Indexing Objects">
<link href="Creating-a-Class.html#Creating-a-Class" rel="prev" title="Creating a Class">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Manipulating-Classes"></a>
<div class="header">
<p>
Next: <a href="Indexing-Objects.html#Indexing-Objects" accesskey="n" rel="next">Indexing Objects</a>, Previous: <a href="Creating-a-Class.html#Creating-a-Class" accesskey="p" rel="prev">Creating a Class</a>, Up: <a href="Object-Oriented-Programming.html#Object-Oriented-Programming" accesskey="u" rel="up">Object Oriented Programming</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Manipulating-Classes-1"></a>
<h3 class="section">34.2 Manipulating Classes</h3>
<p>There are a number of basic classes methods that can be defined to allow
the contents of the classes to be queried and set. The most basic of
these is the <code>display</code> method. The <code>display</code> method is used
by Octave when displaying a class on the screen, due to an expression
that is not terminated with a semicolon. If this method is not defined,
then Octave will printed nothing when displaying the contents of a class.
</p>
<a name="XREFdisplay"></a><dl>
<dt><a name="index-display"></a>Function File: <em></em> <strong>display</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Display the contents of an object. If <var>a</var> is an object of the
class <code>"myclass"</code>, then <code>display</code> is called in a case like
</p>
<div class="example">
<pre class="example">myclass (…)
</pre></div>
<p>where Octave is required to display the contents of a variable of the
type <code>"myclass"</code>.
</p>
<p><strong>See also:</strong> <a href="Built_002din-Data-Types.html#XREFclass">class</a>, <a href="Defining-Indexing-And-Indexed-Assignment.html#XREFsubsref">subsref</a>, <a href="Defining-Indexing-And-Indexed-Assignment.html#XREFsubsasgn">subsasgn</a>.
</p></dd></dl>
<p>An example of a display method for the polynomial class might be
</p>
<div class="example">
<pre class="verbatim">function display (p)
a = p.poly;
first = true;
fprintf ("%s =", inputname (1));
for i = 1 : length (a);
if (a(i) != 0)
if (first)
first = false;
elseif (a(i) > 0)
fprintf (" +");
endif
if (a(i) < 0)
fprintf (" -");
endif
if (i == 1)
fprintf (" %g", abs (a(i)));
elseif (abs(a(i)) != 1)
fprintf (" %g *", abs (a(i)));
endif
if (i > 1)
fprintf (" X");
endif
if (i > 2)
fprintf (" ^ %d", i - 1);
endif
endif
endfor
if (first)
fprintf (" 0");
endif
fprintf ("\n");
endfunction
</pre><pre class="example">
</pre></div>
<p>Note that in the display method, it makes sense to start the method
with the line <code>fprintf ("%s =", inputname (1))</code> to be consistent
with the rest of Octave and print the variable name to be displayed
when displaying the class.
</p>
<p>To be consistent with the Octave graphic handle classes, a class
should also define the <code>get</code> and <code>set</code> methods. The
<code>get</code> method should accept one or two arguments, and given one
argument of the appropriate class it should return a structure with
all of the properties of the class. For example:
</p>
<div class="example">
<pre class="verbatim">function s = get (p, f)
if (nargin == 1)
s.poly = p.poly;
elseif (nargin == 2)
if (ischar (f))
switch (f)
case "poly"
s = p.poly;
otherwise
error ("get: invalid property %s", f);
endswitch
else
error ("get: expecting the property to be a string");
endif
else
print_usage ();
endif
endfunction
</pre><pre class="example">
</pre></div>
<p>Similarly, the <code>set</code> method should taken as its first argument an
object to modify, and then take property/value pairs to be modified.
</p>
<div class="example">
<pre class="verbatim">function s = set (p, varargin)
s = p;
if (length (varargin) < 2 || rem (length (varargin), 2) != 0)
error ("set: expecting property/value pairs");
endif
while (length (varargin) > 1)
prop = varargin{1};
val = varargin{2};
varargin(1:2) = [];
if (ischar (prop) && strcmp (prop, "poly"))
if (isvector (val) && isreal (val))
s.poly = val(:).';
else
error ("set: expecting the value to be a real vector");
endif
else
error ("set: invalid property of polynomial class");
endif
endwhile
endfunction
</pre><pre class="example">
</pre></div>
<p>Note that as Octave does not implement pass by reference, than the
modified object is the return value of the <code>set</code> method and it
must be called like
</p>
<div class="example">
<pre class="example">p = set (p, "a", [1, 0, 0, 0, 1]);
</pre></div>
<p>Also the <code>set</code> method makes use of the <code>subsasgn</code> method of
the class, and this method must be defined. The <code>subsasgn</code> method
is discussed in the next section.
</p>
<p>Finally, user classes can be considered as a special type of a
structure, and so they can be saved to a file in the same manner as a
structure. For example:
</p>
<div class="example">
<pre class="example">p = polynomial ([1, 0, 1]);
save userclass.mat p
clear p
load userclass.mat
</pre></div>
<p>All of the file formats supported by <code>save</code> and <code>load</code> are
supported. In certain circumstances, a user class might either contain
a field that it makes no sense to save or a field that needs to be
initialized before it is saved. This can be done with the
<code>saveobj</code> method of the class
</p>
<a name="XREFsaveobj"></a><dl>
<dt><a name="index-saveobj"></a>Function File: <em><var>b</var> =</em> <strong>saveobj</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Method of a class to manipulate an object prior to saving it to a file.
The function <code>saveobj</code> is called when the object <var>a</var> is saved
using the <code>save</code> function. An example of the use of <code>saveobj</code>
might be to remove fields of the object that don’t make sense to be saved
or it might be used to ensure that certain fields of the object are
initialized before the object is saved. For example:
</p>
<div class="example">
<pre class="example">function b = saveobj (a)
b = a;
if (isempty (b.field))
b.field = initfield (b);
endif
endfunction
</pre></div>
<p><strong>See also:</strong> <a href="#XREFloadobj">loadobj</a>, <a href="Built_002din-Data-Types.html#XREFclass">class</a>.
</p></dd></dl>
<p><code>saveobj</code> is called just prior to saving the class to a
file. Likely, the <code>loadobj</code> method is called just after a class
is loaded from a file, and can be used to ensure that any removed
fields are reinserted into the user object.
</p>
<a name="XREFloadobj"></a><dl>
<dt><a name="index-loadobj"></a>Function File: <em><var>b</var> =</em> <strong>loadobj</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Method of a class to manipulate an object after loading it from a file.
The function <code>loadobj</code> is called when the object <var>a</var> is loaded
using the <code>load</code> function. An example of the use of <code>saveobj</code>
might be to add fields to an object that don’t make sense to be saved.
For example:
</p>
<div class="example">
<pre class="example">function b = loadobj (a)
b = a;
b.addmissingfield = addfield (b);
endfunction
</pre></div>
<p><strong>See also:</strong> <a href="#XREFsaveobj">saveobj</a>, <a href="Built_002din-Data-Types.html#XREFclass">class</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Indexing-Objects.html#Indexing-Objects" accesskey="n" rel="next">Indexing Objects</a>, Previous: <a href="Creating-a-Class.html#Creating-a-Class" accesskey="p" rel="prev">Creating a Class</a>, Up: <a href="Object-Oriented-Programming.html#Object-Oriented-Programming" accesskey="u" rel="up">Object Oriented Programming</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|