/usr/share/doc/octave/octave.html/Creating-a-Class.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 | <!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>Creating a Class (GNU Octave)</title>
<meta name="description" content="Creating a Class (GNU Octave)">
<meta name="keywords" content="Creating a Class (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="Class-Methods.html#Class-Methods" rel="next" title="Class Methods">
<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.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="Creating-a-Class"></a>
<div class="header">
<p>
Next: <a href="Class-Methods.html#Class-Methods" accesskey="n" rel="next">Class Methods</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>This chapter illustrates user-defined classes and object oriented programming
through a custom class designed for polynomials. This class was chosen for
its simplicity which does not distract unnecessarily from the discussion of
the programming features of Octave. Even so, a bit of background on the goals
of the polynomial class is necessary before the syntax and techniques of Octave
object oriented programming are introduced.
</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>This is a sufficient specification to begin writing the constructor for the
polynomial class. All object oriented classes in Octave must be located in a
directory that is the name of the class prepended with the ‘<samp>@</samp>’ symbol.
For example, the polynomial class will have all of its methods defined in the
<samp>@polynomial</samp> directory.
</p>
<p>The constructor for the class must be the name of the class itself; in this
example the constructor resides in the file <samp>@polynomial/polynomial.m</samp>.
Ideally, even when the constructor is called with no arguments it should return
a valid object. A constructor for the polynomial class might look like
</p>
<div class="example">
<pre class="verbatim">## -*- texinfo -*-
## @deftypefn {} {} polynomial ()
## @deftypefnx {} {} 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 > 1)
print_usage ();
endif
if (nargin == 0)
p.poly = [0];
p = class (p, "polynomial");
else
if (strcmp (class (a), "polynomial"))
p = a;
elseif (isreal (a) && isvector (a))
p.poly = a(:).'; # force row vector
p = class (p, "polynomial");
else
error ("@polynomial: A must be a real vector");
endif
endif
endfunction
</pre></div>
<p>Note that the return value of the constructor must be the output of the
<code>class</code> function. The first argument to the <code>class</code> function is a
structure and the second is the name of the class itself. An example of
calling the class constructor to create an instance is
</p>
<div class="example">
<pre class="example">p = polynomial ([1, 0, 1]);
</pre></div>
<p>Methods are defined by m-files in the class directory and can have embedded
documentation the same as any other m-file. The help for the constructor can
be obtained by using the constructor name alone, that is, for the polynomial
constructor <code>help polynomial</code> will return the help string. Help can be
restricted to a particular class by using the class directory name followed
by the method. For example, <code>help @polynomial/polynomial</code> is another
way of displaying the help string for the polynomial constructor. This second
means is the only way to obtain help for the overloaded methods and functions
of a class.
</p>
<p>The same specification mechanism can be used wherever Octave expects a function
name. 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 whether a variable belongs to 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>: <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>, <a href="Introduction-to-Graphics-Structures.html#XREFisprop">isprop</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>: <em></em> <strong>methods</strong> <em>(<var>obj</var>)</em></dt>
<dt><a name="index-methods-1"></a>: <em></em> <strong>methods</strong> <em>("<var>classname</var>")</em></dt>
<dt><a name="index-methods-2"></a>: <em><var>mtds</var> =</em> <strong>methods</strong> <em>(…)</em></dt>
<dd><p>List the names of the public methods for the object <var>obj</var> or the
named class <var>classname</var>.
</p>
<p><var>obj</var> may be an Octave class object or a Java object.
<var>classname</var> may be the name of an Octave class or a Java class.
</p>
<p>When called with no output arguments, <code>methods</code> prints the list of
method names to the screen. Otherwise, the output argument <var>mtds</var>
contains the list in a cell array of strings.
</p>
<p><strong>See also:</strong> <a href="Manipulating-Structures.html#XREFfieldnames">fieldnames</a>.
</p></dd></dl>
<p>To inquire whether a particular method exists for a user class, the
<code>ismethod</code> function can be used.
</p>
<a name="XREFismethod"></a><dl>
<dt><a name="index-ismethod"></a>: <em></em> <strong>ismethod</strong> <em>(<var>obj</var>, <var>method</var>)</em></dt>
<dt><a name="index-ismethod-1"></a>: <em></em> <strong>ismethod</strong> <em>(<var>clsname</var>, <var>method</var>)</em></dt>
<dd><p>Return true if the string <var>method</var> is a valid method of the object
<var>obj</var> or of the class <var>clsname</var>.
</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="Class-Methods.html#Class-Methods" accesskey="n" rel="next">Class Methods</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>
|