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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Class Methods (GNU Octave (version 7.3.0))</title>
<meta name="description" content="Class Methods (GNU Octave (version 7.3.0))">
<meta name="keywords" content="Class Methods (GNU Octave (version 7.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Object-Oriented-Programming.html" rel="up" title="Object Oriented Programming">
<link href="Indexing-Objects.html" rel="next" title="Indexing Objects">
<link href="Creating-a-Class.html" rel="prev" title="Creating a Class">
<style type="text/css">
<!--
a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {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}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
span:hover a.copiable-anchor {visibility: visible}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="section" id="Class-Methods">
<div class="header">
<p>
Next: <a href="Indexing-Objects.html" accesskey="n" rel="next">Indexing Objects</a>, Previous: <a href="Creating-a-Class.html" accesskey="p" rel="prev">Creating a Class</a>, Up: <a href="Object-Oriented-Programming.html" 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" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Class-Methods-1"></span><h3 class="section">34.2 Class Methods</h3>
<p>There are a number of basic class methods that can (and should) be defined to
allow the contents of the classes to be queried and set. The most basic of
these is the <code>disp</code> method. The <code>disp</code> method is used by Octave
whenever a class should be displayed on the screen. Usually this is the result
of an Octave expression that doesn’t end with a semicolon. If this method is
not defined, then Octave won’t print anything when displaying the contents of a
class which can be confusing.
</p>
<p>An example of a <code>disp</code> method for the polynomial class might be
</p>
<div class="example">
<pre class="verbatim">function disp (p)
a = p.poly;
first = true;
for i = 1 : length (a);
if (a(i) != 0)
if (first)
first = false;
elseif (a(i) > 0 || isnan (a(i)))
printf (" +");
endif
if (a(i) < 0)
printf (" -");
endif
if (i == 1)
printf (" %.5g", abs (a(i)));
elseif (abs (a(i)) != 1)
printf (" %.5g *", abs (a(i)));
endif
if (i > 1)
printf (" X");
endif
if (i > 2)
printf (" ^ %d", i - 1);
endif
endif
endfor
if (first)
printf (" 0");
endif
printf ("\n");
endfunction
</pre></div>
<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 accepts
one or two arguments. The first argument is an object of the appropriate
class. If no second argument is given then the method should return a
structure with all the properties of the class. If the optional second
argument is given it should be a property name and the specified property
should be retrieved.
</p>
<div class="example">
<pre class="verbatim">function val = get (p, prop)
if (nargin < 1)
print_usage ();
endif
if (nargin == 1)
val.poly = p.poly;
else
if (! ischar (prop))
error ("@polynomial/get: PROPERTY must be a string");
endif
switch (prop)
case "poly"
val = p.poly;
otherwise
error ('@polynomial/get: invalid PROPERTY "%s"', prop);
endswitch
endif
endfunction
</pre></div>
<p>Similarly, the first argument to the <code>set</code> method should be an object and
any additional arguments should be property/value pairs.
</p>
<div class="example">
<pre class="verbatim">function pout = set (p, varargin)
if (numel (varargin) < 2 || rem (numel (varargin), 2) != 0)
error ("@polynomial/set: expecting PROPERTY/VALUE pairs");
endif
pout = p;
while (numel (varargin) > 1)
prop = varargin{1};
val = varargin{2};
varargin(1:2) = [];
if (! ischar (prop) || ! strcmp (prop, "poly"))
error ("@polynomial/set: invalid PROPERTY for polynomial class");
elseif (! (isreal (val) && isvector (val)))
error ("@polynomial/set: VALUE must be a real vector");
endif
pout.poly = val(:).'; # force row vector
endwhile
endfunction
</pre></div>
<p>Note that Octave does not implement pass by reference; Therefore, to modify an
object requires an assignment statement using the return value from the
<code>set</code> method.
</p>
<div class="example">
<pre class="example">p = set (p, "poly", [1, 0, 0, 0, 1]);
</pre></div>
<p>The <code>set</code> method makes use of the <code>subsasgn</code> method of the class, and
therefore this method must also be defined. The <code>subsasgn</code> method is
discussed more thoroughly in the next section (see <a href="Indexing-Objects.html">Indexing Objects</a>).
</p>
<p>Finally, user classes can be considered to be a special type of a structure,
and 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 contain a field that it doesn’t
make 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>
<span id="XREFsaveobj"></span><dl class="def">
<dt id="index-saveobj"><span class="category">: </span><span><em><var>b</var> =</em> <strong>saveobj</strong> <em>(<var>a</var>)</em><a href='#index-saveobj' class='copiable-anchor'> ¶</a></span></dt>
<dd><p>Method of a class to manipulate an object prior to saving it to a file.
</p>
<p>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. Similarly,
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>
<span id="XREFloadobj"></span><dl class="def">
<dt id="index-loadobj"><span class="category">: </span><span><em><var>b</var> =</em> <strong>loadobj</strong> <em>(<var>a</var>)</em><a href='#index-loadobj' class='copiable-anchor'> ¶</a></span></dt>
<dd><p>Method of a class to manipulate an object after loading it from a file.
</p>
<p>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>
</div>
<hr>
<div class="header">
<p>
Next: <a href="Indexing-Objects.html">Indexing Objects</a>, Previous: <a href="Creating-a-Class.html">Creating a Class</a>, Up: <a href="Object-Oriented-Programming.html">Object Oriented Programming</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|