29 lines
18 KiB
HTML
29 lines
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 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,"tocview_0");">►</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 </td><td><a href="model.html" class="tocviewlink" data-pltdoc="x">Language Model</a></td></tr><tr><td align="right">2 </td><td><a href="notation.html" class="tocviewlink" data-pltdoc="x">Notation for Documentation</a></td></tr><tr><td align="right">3 </td><td><a href="syntax.html" class="tocviewlink" data-pltdoc="x">Syntactic Forms</a></td></tr><tr><td align="right">4 </td><td><a href="data.html" class="tocviewlink" data-pltdoc="x">Datatypes</a></td></tr><tr><td align="right">5 </td><td><a href="structures.html" class="tocviewlink" data-pltdoc="x">Structures</a></td></tr><tr><td align="right">6 </td><td><a href="mzlib_class.html" class="tocviewselflink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">7 </td><td><a href="mzlib_unit.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">8 </td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">9 </td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">10 </td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Control Flow</a></td></tr><tr><td align="right">11 </td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Parallelism</a></td></tr><tr><td align="right">12 </td><td><a href="Macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">13 </td><td><a href="input-and-output.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">14 </td><td><a href="security.html" class="tocviewlink" data-pltdoc="x">Reflection and Security</a></td></tr><tr><td align="right">15 </td><td><a href="os.html" class="tocviewlink" data-pltdoc="x">Operating System</a></td></tr><tr><td align="right">16 </td><td><a href="memory.html" class="tocviewlink" data-pltdoc="x">Memory Management</a></td></tr><tr><td align="right">17 </td><td><a href="unsafe.html" class="tocviewlink" data-pltdoc="x">Unsafe Operations</a></td><
|
||
|
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’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’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 “meta-classes” or
|
||
|
“meta-interfaces”).</p><table cellspacing="0" cellpadding="0"><tr><td><p><span class="hspace"> </span><a href="createinterface.html" class="toclink" data-pltdoc="x">6.1<span class="hspace"> </span>Creating Interfaces</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="createclass.html" class="toclink" data-pltdoc="x">6.2<span class="hspace"> </span>Creating Classes</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="createclass.html#%28part._clinitvars%29" class="toclink" data-pltdoc="x">6.2.1<span class="hspace"> </span>Initialization Variables</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="createclass.html#%28part._clfields%29" class="toclink" data-pltdoc="x">6.2.2<span class="hspace"> </span>Fields</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="createclass.html#%28part._clmethods%29" class="toclink" data-pltdoc="x">6.2.3<span class="hspace"> </span>Methods</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="createclass.html#%28part._clmethoddefs%29" class="toclink" data-pltdoc="x">6.2.3.1<span class="hspace"> </span>Method Definitions</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="createclass.html#%28part._classinherit%29" class="toclink" data-pltdoc="x">6.2.3.2<span class="hspace"> </span>Inherited and Superclass Methods</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="createclass.html#%28part._extnames%29" class="toclink" data-pltdoc="x">6.2.3.3<span class="hspace"> </span>Internal and External Names</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="objcreation.html" class="toclink" data-pltdoc="x">6.3<span class="hspace"> </span>Creating Objects</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="ivaraccess.html" class="toclink" data-pltdoc="x">6.4<span class="hspace"> </span>Field and Method Access</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="ivaraccess.html#%28part._methodcalls%29" class="toclink" data-pltdoc="x">6.4.1<span class="hspace"> </span>Methods</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="ivaraccess.html#%28part._.Fields%29" class="toclink" data-pltdoc="x">6.4.2<span class="hspace"> </span>Fields</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="ivaraccess.html#%28part._.Generics%29" class="toclink" data-pltdoc="x">6.4.3<span class="hspace"> </span>Generics</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="mixins.html" class="toclink" data-pltdoc="x">6.5<span class="hspace"> </span>Mixins</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="trait.html" class="toclink" data-pltdoc="x">6.6<span class="hspace"> </span>Traits</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="Object_and_Class_Contracts.html" class="toclink" data-pltdoc="x">6.7<span class="hspace"> </span>Object and Class Contracts</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="objectequality.html" class="toclink" data-pltdoc="x">6.8<span class="hspace"> </span>Object Equality and Hashing</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="objectserialize.html" class="toclink" data-pltdoc="x">6.9<span class="hspace"> </span>Object Serialization</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="objectprinting.html" class="toclink" data-pltdoc="x">6.10<span class="hspace"> </s
|