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
|
<!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: Creating a Class</title>
<meta name="description" content="GNU Octave: Creating a Class">
<meta name="keywords" content="GNU Octave: Creating a Class">
<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="Manipulating-Classes.html#Manipulating-Classes" rel="next" title="Manipulating Classes">
<link href="Object-Oriented-Programming.html#Object-Oriented-Programming" rel="prev" title="Object Oriented Programming">
<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="Creating-a-Class"></a>
<div class="header">
<p>
Next: <a href="Manipulating-Classes.html#Manipulating-Classes" accesskey="n" rel="next">Manipulating Classes</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="Creating-a-Class-1"></a>
<h3 class="section">34.1 Creating a Class</h3>
<p>We use in the following text a polynomial class to demonstrate the use
of object oriented programming within Octave. This class was chosen as
it is simple, and so doesn’t distract unnecessarily from the
discussion of the programming features of Octave. However, even still
a small understand of the polynomial class itself is necessary to
fully grasp the techniques described.
</p>
<p>The polynomial class is used to represent polynomials of the form
</p>
<div class="example">
<pre class="example">a0 + a1 * x + a2 * x^2 + … + an * x^n
</pre></div>
<p>where
a0, a1, etc. are real scalars.
Thus the polynomial can be represented by a vector
</p>
<div class="example">
<pre class="example">a = [a0, a1, a2, …, an];
</pre></div>
<p>We therefore now have sufficient information about the requirements of
the class constructor for our polynomial class to write it. All object
oriented classes in Octave, must be contained with a directory taking
the name of the class, prepended with the @ symbol. For example, with
our polynomial class, we would place the methods defining the class in
the @polynomial directory.
</p>
<p>The constructor of the class, must have the name of the class itself
and so in our example the constructor with have the name
<samp>@polynomial/polynomial.m</samp>. Also ideally when the constructor is
called with no arguments to should return a value object. So for example
our polynomial might look like
</p>
<div class="example">
<pre class="verbatim">## -*- texinfo -*-
## @deftypefn {Function File} {} polynomial ()
## @deftypefnx {Function File} {} polynomial (@var{a})
## Create a polynomial object representing the polynomial
##
## @example
## a0 + a1 * x + a2 * x^2 + @dots{} + an * x^n
## @end example
##
## @noindent
## from a vector of coefficients [a0 a1 a2 @dots{} an].
## @end deftypefn
function p = polynomial (a)
if (nargin == 0)
p.poly = [0];
p = class (p, "polynomial");
elseif (nargin == 1)
if (strcmp (class (a), "polynomial"))
p = a;
elseif (isvector (a) && isreal (a))
p.poly = a(:).';
p = class (p, "polynomial");
else
error ("polynomial: expecting real vector");
endif
else
print_usage ();
endif
endfunction
</pre><pre class="example">
</pre></div>
<p>Note that the return value of the constructor must be the output of
the <code>class</code> function called with the first argument being a
structure and the second argument being the class name. An example of
the call to this constructor function is then
</p>
<div class="example">
<pre class="example">p = polynomial ([1, 0, 1]);
</pre></div>
<p>Note that methods of a class can be documented. The help for the
constructor itself can be obtained with the constructor name, that is
for the polynomial constructor <code>help polynomial</code> will return the
help string. Also the help can be obtained by restricting the search
for the help to a particular class, for example <code>help
@polynomial/polynomial</code>. This second method is the only means of
getting help for the overloaded methods and functions of the class.
</p>
<p>The same is true for other Octave functions that take a function name
as an argument. For example <code>type @polynomial/display</code> will
print the code of the display method of the polynomial class to the
screen, and <code>dbstop @polynomial/display</code> will set a breakpoint
at the first executable line of the display method of the polynomial
class.
</p>
<p>To check where a variable is a user class, the <code>isobject</code> and
<code>isa</code> functions can be used. For example:
</p>
<div class="example">
<pre class="example">p = polynomial ([1, 0, 1]);
isobject (p)
⇒ 1
isa (p, "polynomial")
⇒ 1
</pre></div>
<a name="XREFisobject"></a><dl>
<dt><a name="index-isobject"></a>Built-in Function: <em></em> <strong>isobject</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a class object.
</p>
<p><strong>See also:</strong> <a href="Built_002din-Data-Types.html#XREFclass">class</a>, <a href="Data-Types.html#XREFtypeinfo">typeinfo</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>, <a href="#XREFismethod">ismethod</a>.
</p></dd></dl>
<p>The available methods of a class can be displayed with the
<code>methods</code> function.
</p>
<a name="XREFmethods"></a><dl>
<dt><a name="index-methods"></a>Function File: <em></em> <strong>methods</strong> <em>(<var>obj</var>)</em></dt>
<dt><a name="index-methods-1"></a>Function File: <em></em> <strong>methods</strong> <em>("<var>classname</var>")</em></dt>
<dt><a name="index-methods-2"></a>Function File: <em><var>mtds</var> =</em> <strong>methods</strong> <em>(…)</em></dt>
<dd>
<p>Return a cell array containing the names of the methods for the
object <var>obj</var> or the named class <var>classname</var>.
<var>obj</var> may be an Octave class object or a Java object.
</p>
<p><strong>See also:</strong> <a href="Manipulating-Structures.html#XREFfieldnames">fieldnames</a>.
</p></dd></dl>
<p>To inquire whether a particular method is available to a user class, the
<code>ismethod</code> function can be used.
</p>
<a name="XREFismethod"></a><dl>
<dt><a name="index-ismethod"></a>Built-in Function: <em></em> <strong>ismethod</strong> <em>(<var>x</var>, <var>method</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a class object and the string <var>method</var>
is a method of this class.
</p>
<p><strong>See also:</strong> <a href="Introduction-to-Graphics-Structures.html#XREFisprop">isprop</a>, <a href="#XREFisobject">isobject</a>.
</p></dd></dl>
<p>For example:
</p>
<div class="example">
<pre class="example">p = polynomial ([1, 0, 1]);
ismethod (p, "roots")
⇒ 1
</pre></div>
<hr>
<div class="header">
<p>
Next: <a href="Manipulating-Classes.html#Manipulating-Classes" accesskey="n" rel="next">Manipulating Classes</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>
|