emacs.d/clones/docs.racket-lang.org/reference/mzlib_class.html
2022-08-24 19:36:32 +02:00

29 lines
No EOL
18 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>6&nbsp;Classes and Objects</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="extras.css" title="default"/><link rel="stylesheet" type="text/css" href="icons.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_0&quot;);">&#9658;</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Reference</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1&nbsp;</td><td><a href="model.html" class="tocviewlink" data-pltdoc="x">Language Model</a></td></tr><tr><td align="right">2&nbsp;</td><td><a href="notation.html" class="tocviewlink" data-pltdoc="x">Notation for Documentation</a></td></tr><tr><td align="right">3&nbsp;</td><td><a href="syntax.html" class="tocviewlink" data-pltdoc="x">Syntactic Forms</a></td></tr><tr><td align="right">4&nbsp;</td><td><a href="data.html" class="tocviewlink" data-pltdoc="x">Datatypes</a></td></tr><tr><td align="right">5&nbsp;</td><td><a href="structures.html" class="tocviewlink" data-pltdoc="x">Structures</a></td></tr><tr><td align="right">6&nbsp;</td><td><a href="mzlib_class.html" class="tocviewselflink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">7&nbsp;</td><td><a href="mzlib_unit.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">8&nbsp;</td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">9&nbsp;</td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">10&nbsp;</td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Control Flow</a></td></tr><tr><td align="right">11&nbsp;</td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Parallelism</a></td></tr><tr><td align="right">12&nbsp;</td><td><a href="Macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">13&nbsp;</td><td><a href="input-and-output.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">14&nbsp;</td><td><a href="security.html" class="tocviewlink" data-pltdoc="x">Reflection and Security</a></td></tr><tr><td align="right">15&nbsp;</td><td><a href="os.html" class="tocviewlink" data-pltdoc="x">Operating System</a></td></tr><tr><td align="right">16&nbsp;</td><td><a href="memory.html" class="tocviewlink" data-pltdoc="x">Memory Management</a></td></tr><tr><td align="right">17&nbsp;</td><td><a href="unsafe.html" class="tocviewlink" data-pltdoc="x">Unsafe Operations</a></td></tr><tr><td align="right">18&nbsp;</td><td><a href="running.html" class="tocviewlink" data-pltdoc="x">Running Racket</a></td></tr><tr><td align="right"></td><td><a href="doc-bibliography.html" class="tocviewlink" data-pltdoc="x">Bibliography</a></td></tr><tr><td align="right"></td><td><a href="doc-index.html" class="tocviewlink" data-pltdoc="x">Index</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_1&quot;);">&#9660;</a></td><td>6&nbsp;</td><td><a href="mzlib_class.html" class="tocviewselflink" data-pltdoc="x">Classes and Objects</a></td></tr></table><div class="tocviewsublistbottom" style="display: block;" id="tocview_1"><table cellspacing="0" cellpadding="0"><tr><td align="right">6.1&nbsp;</td><td><a href="createinterface.html" class="tocviewlink" data-pltdoc="x">Creating Interfaces</a></td></tr><tr><td align="right">6.2&nbsp;</td><td><a href="createclass.html" class="tocviewlink" data-pltdoc="x">Creating Classes</a></td></tr><tr><td align="right">6.3&nbsp;</td><td><a href="objcreation.html" class="tocviewlink" data-pltdoc="x">Creating Objects</a></td></tr><tr><td align="right">6.4&nbsp;</td><td><a href="ivaraccess.html" class="tocviewlink" data-pltdoc="x">Field and Method Access</a></td></tr><tr><td align="right">6.5&nbsp;</td><td><a href="mixins.html" class="tocviewlink" data-pltdoc="x">Mixins</a></td></tr><tr><td align="right">6.6&nbsp;</td><td><a href="trait.html" class="tocviewlink" data-pltdoc="x">Traits</a></td></tr><tr><td align="right">6.7&nbsp;</td><td><a href="Object_and_Class_Contracts.html" class="tocviewlink" data-pltdoc="x">Object and Class Contracts</a></td></tr><tr><td align="right">6.8&nbsp;</td><td><a href="objectequality.html" class="tocviewlink" data-pltdoc="x">Object Equality and Hashing</a></td></tr><tr><td align="right">6.9&nbsp;</td><td><a href="objectserialize.html" class="tocviewlink" data-pltdoc="x">Object Serialization</a></td></tr><tr><td align="right">6.10&nbsp;</td><td><a href="objectprinting.html" class="tocviewlink" data-pltdoc="x">Object Printing</a></td></tr><tr><td align="right">6.11&nbsp;</td><td><a href="objectutils.html" class="tocviewlink" data-pltdoc="x">Object, Class, and Interface Utilities</a></td></tr><tr><td align="right">6.12&nbsp;</td><td><a href="Surrogates.html" class="tocviewlink" data-pltdoc="x">Surrogates</a></td></tr></table></div></div></div></div><div class="maincolumn"><div class="main"><div class="navsettop"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, &quot;8.6&quot;, &quot;../&quot;);"/></form>&nbsp;&nbsp;<a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot(&quot;8.6&quot;);">top</a><span class="tocsettoggle">&nbsp;&nbsp;<a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright">&nbsp;&nbsp;<a href="structinfo.html" title="backward to &quot;5.7 Structure Type Transformer Binding&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="index.html" title="up to &quot;The Racket Reference&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="createinterface.html" title="forward to &quot;6.1 Creating Interfaces&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div><h3 x-source-module="(lib &quot;scribblings/reference/reference.scrbl&quot;)" x-source-pkg="racket-doc" x-part-tag="&quot;mzlib:class&quot;">6<tt>&nbsp;</tt><a name="(part._mzlib~3aclass)"></a><a name="(mod-path._racket/class)"></a>Classes and Objects</h3><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=guide&amp;rel=classes.html&amp;version=8.6" class="Sq" data-pltdoc="x">Classes and Objects</a> in <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=guide&amp;rel=index.html&amp;version=8.6" class="Sq" data-pltdoc="x">The Racket Guide</a> introduces classes and objects.</p></blockquote></blockquote></blockquote><p><div class="SIntrapara"><table cellspacing="0" cellpadding="0" class="defmodule"><tr><td align="left"><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29" class="RktStxLink" data-pltdoc="x">require</a></span><span class="stt"> </span><a href="mzlib_class.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">racket/class</span></a><span class="RktPn">)</span></td><td align="right"><span class="RpackageSpec"><span class="Smaller">&nbsp;package:</span> <a href="https://pkgs.racket-lang.org/package/base" title="Install this package using `raco pkg install base`"><span class="stt">base</span></a></span></td></tr></table></div><div class="SIntrapara">The bindings documented in this section are provided by the <a href="mzlib_class.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">racket/class</span></a> and <a href="index.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">racket</span></a> libraries, but not <a href="index.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">racket/base</span></a>.</div></p><p>A <a name="(tech._clas)"></a><span style="font-style: italic">class</span> specifies</p><ul><li><p>a collection of fields;</p></li><li><p>a collection of methods;</p></li><li><p>initial value expressions for the fields; and</p></li><li><p>initialization variables that are bound to initialization
arguments.</p></li></ul><p>In the context of the class system, an <span style="font-style: italic">object</span> is a
collection of bindings for fields that are instantiated according to a
class description.</p><p>The class system allows a program to define a new class (a
<a name="(tech._derived._clas)"></a><span style="font-style: italic">derived class</span>) in terms of an existing class (the
<a name="(tech._superclas)"></a><span style="font-style: italic">superclass</span>) using inheritance, overriding, and augmenting:</p><ul><li><p><a name="(tech._inheritance)"></a><span style="font-style: italic">inheritance</span>: An object of a derived class supports
methods and instantiates fields declared by the derived class&rsquo;s
superclass, as well as methods and fields declared in the derived
class expression.</p></li><li><p><a name="(tech._overriding)"></a><span style="font-style: italic">overriding</span>: Some methods declared in a superclass can
be replaced in the derived class. References to the overridden method
in the superclass use the implementation in the derived class.</p></li><li><p><a name="(tech._augmenting)"></a><span style="font-style: italic">augmenting</span>: Some methods declared in a superclass can
be merely extended in the derived class. The superclass method
specifically delegates to the augmenting method in the derived class.</p></li></ul><p>An <a name="(tech._interface)"></a><span style="font-style: italic">interface</span> is a collection of method names to be
implemented by a class, combined with a derivation requirement. A
class <a name="(tech._implement)"></a><span style="font-style: italic">implements</span> an interface when it</p><ul><li><p>declares (or inherits) a public method for each variable in the
interface;</p></li><li><p>is derived from the class required by the interface, if any; and</p></li><li><p>specifically declares its intention to implement the interface.</p></li></ul><p>A class can implement any number of interfaces. A derived class
automatically implements any interface that its superclass
implements. Each class also implements an implicitly-defined interface
that is associated with the class. The implicitly-defined interface
contains all of the class&rsquo;s public method names, and it requires that
all other implementations of the interface are derived from the class.</p><p>A new interface can <a name="(tech._extend)"></a><span style="font-style: italic">extend</span> one or more interfaces with
additional method names; each class that implements the extended
interface also implements the original interfaces. The derivation
requirements of the original interface must be consistent, and the
extended interface inherits the most specific derivation requirement
from the original interfaces.</p><p>Classes, objects, and interfaces are all values. However, a class or
interface is not an object (i.e., there are no &ldquo;meta-classes&rdquo; or
&ldquo;meta-interfaces&rdquo;).</p><table cellspacing="0" cellpadding="0"><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="createinterface.html" class="toclink" data-pltdoc="x">6.1<span class="hspace">&nbsp;</span>Creating Interfaces</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="createclass.html" class="toclink" data-pltdoc="x">6.2<span class="hspace">&nbsp;</span>Creating Classes</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="createclass.html#%28part._clinitvars%29" class="toclink" data-pltdoc="x">6.2.1<span class="hspace">&nbsp;</span>Initialization Variables</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="createclass.html#%28part._clfields%29" class="toclink" data-pltdoc="x">6.2.2<span class="hspace">&nbsp;</span>Fields</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="createclass.html#%28part._clmethods%29" class="toclink" data-pltdoc="x">6.2.3<span class="hspace">&nbsp;</span>Methods</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="createclass.html#%28part._clmethoddefs%29" class="toclink" data-pltdoc="x">6.2.3.1<span class="hspace">&nbsp;</span>Method Definitions</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="createclass.html#%28part._classinherit%29" class="toclink" data-pltdoc="x">6.2.3.2<span class="hspace">&nbsp;</span>Inherited and Superclass Methods</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="createclass.html#%28part._extnames%29" class="toclink" data-pltdoc="x">6.2.3.3<span class="hspace">&nbsp;</span>Internal and External Names</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="objcreation.html" class="toclink" data-pltdoc="x">6.3<span class="hspace">&nbsp;</span>Creating Objects</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="ivaraccess.html" class="toclink" data-pltdoc="x">6.4<span class="hspace">&nbsp;</span>Field and Method Access</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="ivaraccess.html#%28part._methodcalls%29" class="toclink" data-pltdoc="x">6.4.1<span class="hspace">&nbsp;</span>Methods</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="ivaraccess.html#%28part._.Fields%29" class="toclink" data-pltdoc="x">6.4.2<span class="hspace">&nbsp;</span>Fields</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="ivaraccess.html#%28part._.Generics%29" class="toclink" data-pltdoc="x">6.4.3<span class="hspace">&nbsp;</span>Generics</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="mixins.html" class="toclink" data-pltdoc="x">6.5<span class="hspace">&nbsp;</span>Mixins</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="trait.html" class="toclink" data-pltdoc="x">6.6<span class="hspace">&nbsp;</span>Traits</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="Object_and_Class_Contracts.html" class="toclink" data-pltdoc="x">6.7<span class="hspace">&nbsp;</span>Object and Class Contracts</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="objectequality.html" class="toclink" data-pltdoc="x">6.8<span class="hspace">&nbsp;</span>Object Equality and Hashing</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="objectserialize.html" class="toclink" data-pltdoc="x">6.9<span class="hspace">&nbsp;</span>Object Serialization</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="objectprinting.html" class="toclink" data-pltdoc="x">6.10<span class="hspace">&nbsp;</span>Object Printing</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="objectutils.html" class="toclink" data-pltdoc="x">6.11<span class="hspace">&nbsp;</span>Object, Class, and Interface Utilities</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="Surrogates.html" class="toclink" data-pltdoc="x">6.12<span class="hspace">&nbsp;</span>Surrogates</a></p></td></tr></table><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, &quot;8.6&quot;, &quot;../&quot;);"/></form>&nbsp;&nbsp;<a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot(&quot;8.6&quot;);">top</a><span class="tocsettoggle">&nbsp;&nbsp;<a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright">&nbsp;&nbsp;<a href="structinfo.html" title="backward to &quot;5.7 Structure Type Transformer Binding&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="index.html" title="up to &quot;The Racket Reference&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="createinterface.html" title="forward to &quot;6.1 Creating Interfaces&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div></div></div><div id="contextindicator">&nbsp;</div></body></html>