/usr/share/doc/octave/octave.html/Class-Methods.html is in octave-doc 4.2.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Class Methods (GNU Octave)</title>
<meta name="description" content="Class Methods (GNU Octave)">
<meta name="keywords" content="Class Methods (GNU Octave)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<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.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
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.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<a name="Class-Methods"></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="Class-Methods-1"></a>
<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>display</code> method. The <code>display</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>
<a name="XREFdisplay"></a><dl>
<dt><a name="index-display"></a>: <em></em> <strong>display</strong> <em>(<var>obj</var>)</em></dt>
<dd><p>Display the contents of the object <var>obj</var>.
</p>
<p>The Octave interpreter calls the <code>display</code> function whenever it needs
to present a class on-screen. Typically, this would be a statement which
does not end in a semicolon to suppress output. For example:
</p>
<div class="example">
<pre class="example">myobj = myclass (…)
</pre></div>
<p>User-defined classes should overload the <code>display</code> method so that
something useful is printed for a class object. Otherwise, Octave will
report only that the object is an instance of its class.
</p>
<div class="example">
<pre class="example">myobj = myclass (…)
⇒ myobj = <class myclass>
</pre></div>
<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)
printf ("%s =", inputname (1));
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>Note that in the display method it makes sense to start the method with the
line <code>printf ("%s =", inputname (1))</code><!-- /@w --> to be consistent with the rest
of Octave which prints the variable name to be displayed followed by the value.
</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 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 || nargin > 2)
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">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>
<a name="XREFsaveobj"></a><dl>
<dt><a name="index-saveobj"></a>: <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.
</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>
<a name="XREFloadobj"></a><dl>
<dt><a name="index-loadobj"></a>: <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.
</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>
<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>
|