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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Inheritance and Aggregation (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Inheritance and Aggregation (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Inheritance and Aggregation (GNU Octave (version 10.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="classdef-Classes.html" rel="next" title="classdef Classes">
<link href="Overloading-Objects.html" rel="prev" title="Overloading Objects">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="section-level-extent" id="Inheritance-and-Aggregation">
<div class="nav-panel">
<p>
Next: <a href="classdef-Classes.html" accesskey="n" rel="next"><code class="code">classdef</code> Classes</a>, Previous: <a href="Overloading-Objects.html" accesskey="p" rel="prev">Overloading Objects</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>
<h3 class="section" id="Inheritance-and-Aggregation-1"><span>34.5 Inheritance and Aggregation<a class="copiable-link" href="#Inheritance-and-Aggregation-1"> ¶</a></span></h3>
<p>Using classes to build new classes is supported by Octave through the use of
both inheritance and aggregation.
</p>
<p>Class inheritance is provided by Octave using the <code class="code">class</code> function in the
class constructor. As in the case of the polynomial class, the Octave
programmer will create a structure that contains the data fields required by
the class, and then call the <code class="code">class</code> function to indicate that an object
is to be created from the structure. Creating a child of an existing object is
done by creating an object of the parent class and providing that object as the
third argument of the class function.
</p>
<p>This is most easily demonstrated by example. Suppose the programmer needs a
FIR filter, i.e., a filter with a numerator polynomial but a denominator of 1.
In traditional Octave programming this would be performed as follows.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">>> x = [some data vector];
>> n = [some coefficient vector];
>> y = filter (n, 1, x);
</pre></div></div>
<p>The equivalent behavior can be implemented as a class <code class="code">@FIRfilter</code>. The
constructor for this class is the file <samp class="file">FIRfilter.m</samp> in the class
directory <samp class="file">@FIRfilter</samp>.
</p>
<div class="example">
<pre class="verbatim">## -*- texinfo -*-
## @deftypefn {} {} FIRfilter ()
## @deftypefnx {} {} FIRfilter (@var{p})
## Create a FIR filter with polynomial @var{p} as coefficient vector.
## @end deftypefn
function f = FIRfilter (p)
if (nargin == 0)
p = @polynomial ([1]);
elseif (! isa (p, "polynomial"))
error ("@FIRfilter: P must be a polynomial object");
endif
f.polynomial = [];
f = class (f, "FIRfilter", p);
endfunction
</pre></div>
<p>As before, the leading comments provide documentation for the class
constructor. This constructor is very similar to the polynomial class
constructor, except that a polynomial object is passed as the third argument to
the <code class="code">class</code> function, telling Octave that the <code class="code">FIRfilter</code> class will
be derived from the polynomial class. The FIR filter class itself does not
have any data fields, but it must provide a struct to the <code class="code">class</code>
function. Given that the <code class="code">@polynomial</code> constructor will add an element
named <var class="var">polynomial</var> to the object struct, the <code class="code">@FIRfilter</code> just
initializes a struct with a dummy field <var class="var">polynomial</var> which will later be
overwritten.
</p>
<p>Note that the sample code always provides for the case in which no arguments
are supplied. This is important because Octave will call a constructor with
no arguments when loading objects from saved files in order to determine the
inheritance structure.
</p>
<p>A class may be a child of more than one class (see <a class="pxref" href="Built_002din-Data-Types.html#XREFclass">class</a>), and
inheritance may be nested. There is no limitation to the number of parents or
the level of nesting other than memory or other physical issues.
</p>
<p>For the <code class="code">FIRfilter</code> class, more control about the object display is
desired. Therefore, the <code class="code">display</code> method rather than the <code class="code">disp</code>
method is overloaded (see <a class="pxref" href="Class-Methods.html">Class Methods</a>). A simple example might be
</p>
<div class="example">
<div class="group"><pre class="verbatim">function display (f)
printf ("%s.polynomial", inputname (1));
disp (f.polynomial);
endfunction
</pre></div></div>
<p>Note that the <code class="code">FIRfilter</code>’s display method relies on the <code class="code">disp</code>
method from the <code class="code">polynomial</code> class to actually display the filter
coefficients. Furthermore, note that in the <code class="code">display</code> method it makes
sense to start the method with the line
<code class="code"><code class="code">printf ("%s =", inputname (1))</code></code> to be consistent with the
rest of Octave which prints the variable name to be displayed followed by the
value. In general it is not recommended to overload the <code class="code">display</code>
function.
</p>
<a class="anchor" id="XREFdisplay"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-display"><span><strong class="def-name">display</strong> <code class="def-code-arguments">(<var class="var">obj</var>)</code><a class="copiable-link" href="#index-display"> ¶</a></span></dt>
<dd><p>Display the contents of the object <var class="var">obj</var> prepended by its name.
</p>
<p>The Octave interpreter calls the <code class="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-preformatted">myclass (...)
</pre></div>
<p>Or:
</p>
<div class="example">
<pre class="example-preformatted">myobj = myclass (...)
</pre></div>
<p>In general, user-defined classes should overload the <code class="code">disp</code> method to
avoid the default output:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">myobj = myclass (...)
⇒ myobj =
<class myclass>
</pre></div></div>
<p>When overloading the <code class="code">display</code> method instead, one has to take care
of properly displaying the object’s name. This can be done by using the
<code class="code">inputname</code> function.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Terminal-Output.html#XREFdisp">disp</a>, <a class="ref" href="Built_002din-Data-Types.html#XREFclass">class</a>, <a class="ref" href="Defining-Indexing-And-Indexed-Assignment.html#XREFsubsref">subsref</a>, <a class="ref" href="Defining-Indexing-And-Indexed-Assignment.html#XREFsubsasgn">subsasgn</a>.
</p></dd></dl>
<p>Once a constructor and display method exist, it is possible to create an
instance of the class. It is also possible to check the class type and examine
the underlying structure.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">octave:1> f = FIRfilter (polynomial ([1 1 1]/3))
f.polynomial = 0.33333 + 0.33333 * X + 0.33333 * X ^ 2
octave:2> class (f)
ans = FIRfilter
octave:3> isa (f, "FIRfilter")
ans = 1
octave:4> isa (f, "polynomial")
ans = 1
octave:5> struct (f)
ans =
scalar structure containing the fields:
polynomial = 0.33333 + 0.33333 * X + 0.33333 * X ^ 2
</pre></div></div>
<p>The only thing remaining to make this class usable is a method for processing
data. But before that, it is usually desirable to also have a way of changing
the data stored in a class. Since the fields in the underlying struct are
private by default, it is necessary to provide a mechanism to access the
fields. The <code class="code">subsref</code> method may be used for both tasks.
</p>
<div class="example smallexample">
<pre class="verbatim">function r = subsref (f, x)
switch (x.type)
case "()"
n = f.polynomial;
r = filter (n.poly, 1, x.subs{1});
case "."
fld = x.subs;
if (! strcmp (fld, "polynomial"))
error ('@FIRfilter/subsref: invalid property "%s"', fld);
endif
r = f.polynomial;
otherwise
error ("@FIRfilter/subsref: invalid subscript type for FIR filter");
endswitch
endfunction
</pre></div>
<p>The <code class="code">"()"</code> case allows us to filter data using the polynomial provided
to the constructor.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">octave:2> f = FIRfilter (polynomial ([1 1 1]/3));
octave:3> x = ones (5,1);
octave:4> y = f(x)
y =
0.33333
0.66667
1.00000
1.00000
1.00000
</pre></div></div>
<p>The <code class="code">"."</code> case allows us to view the contents of the polynomial field.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">octave:1> f = FIRfilter (polynomial ([1 1 1]/3));
octave:2> f.polynomial
ans = 0.33333 + 0.33333 * X + 0.33333 * X ^ 2
</pre></div></div>
<p>In order to change the contents of the object a <code class="code">subsasgn</code> method is
needed. For example, the following code makes the polynomial field publicly
writable
</p>
<div class="example">
<div class="group"><pre class="verbatim">function fout = subsasgn (f, index, val)
switch (index.type)
case "."
fld = index.subs;
if (! strcmp (fld, "polynomial"))
error ('@FIRfilter/subsasgn: invalid property "%s"', fld);
endif
fout = f;
fout.polynomial = val;
otherwise
error ("@FIRfilter/subsasgn: Invalid index type")
endswitch
endfunction
</pre></div></div>
<p>so that
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">octave:1> f = FIRfilter ();
octave:2> f.polynomial = polynomial ([1 2 3])
f.polynomial = 1 + 2 * X + 3 * X ^ 2
</pre></div></div>
<p>Defining the FIRfilter<!-- /@w --> class as a child of the polynomial class implies
that a FIRfilter<!-- /@w --> object may be used any place that a polynomial object may
be used. This is not a normal use of a filter. It may be a more sensible
design approach to use aggregation rather than inheritance. In this case, the
polynomial is simply a field in the class structure. A class constructor for
the aggregation case might be
</p>
<div class="example">
<pre class="verbatim">## -*- texinfo -*-
## @deftypefn {} {} FIRfilter ()
## @deftypefnx {} {} FIRfilter (@var{p})
## Create a FIR filter with polynomial @var{p} as coefficient vector.
## @end deftypefn
function f = FIRfilter (p)
if (nargin == 0)
f.polynomial = @polynomial ([1]);
else
if (! isa (p, "polynomial"))
error ("@FIRfilter: P must be a polynomial object");
endif
f.polynomial = p;
endif
f = class (f, "FIRfilter");
endfunction
</pre></div>
<p>For this example only the constructor needs changing, and all other class
methods stay the same.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="classdef-Classes.html"><code class="code">classdef</code> Classes</a>, Previous: <a href="Overloading-Objects.html">Overloading Objects</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>
|