53 lines
25 KiB
HTML
53 lines
25 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.3 Creating 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="../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></tr><tr><td align="right">18 </td><td><a href="running.html" class="t
|
||
|
by-position initialization arguments, the <span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._new%29%29" class="RktStxLink" data-pltdoc="x">new</a></span> form
|
||
|
creates a new object with by-name initialization arguments, and
|
||
|
the <span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._instantiate%29%29" class="RktStxLink" data-pltdoc="x">instantiate</a></span> form creates a new object with both
|
||
|
by-position and by-name initialization arguments.</p><p>All fields in the newly created object are initially bound to the
|
||
|
special <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=guide&rel=void_undefined.html&version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="nobreak"><span class="RktRes">#<undefined></span></span></a> value (see
|
||
|
<a href="void.html" data-pltdoc="x">Void</a>). Initialization variables with default value
|
||
|
expressions (and no provided value) are also initialized to
|
||
|
<a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=guide&rel=void_undefined.html&version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="nobreak"><span class="RktRes">#<undefined></span></span></a>. After argument values are assigned to
|
||
|
initialization variables, expressions in <span class="RktSym"><a href="createclass.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._field%29%29" class="RktStxLink" data-pltdoc="x">field</a></span> clauses,
|
||
|
<span class="RktSym"><a href="createclass.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._init-field%29%29" class="RktStxLink" data-pltdoc="x">init-field</a></span> clauses with no provided argument,
|
||
|
<span class="RktSym"><a href="createclass.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._init%29%29" class="RktStxLink" data-pltdoc="x">init</a></span> clauses with no provided argument, private field
|
||
|
definitions, and other expressions are evaluated. Those
|
||
|
expressions are evaluated as they appear in the class expression,
|
||
|
from left to right.</p><p>Sometime during the evaluation of the expressions,
|
||
|
superclass-declared initializations must be evaluated once by
|
||
|
using the <span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-make-object%29%29" class="RktStxLink" data-pltdoc="x">super-make-object</a></span> procedure,
|
||
|
<span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-new%29%29" class="RktStxLink" data-pltdoc="x">super-new</a></span> form, or <span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-instantiate%29%29" class="RktStxLink" data-pltdoc="x">super-instantiate</a></span> form.</p><p>By-name initialization arguments to a class that have no matching
|
||
|
initialization variable are implicitly added as by-name arguments
|
||
|
to a <span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-make-object%29%29" class="RktStxLink" data-pltdoc="x">super-make-object</a></span>, <span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-new%29%29" class="RktStxLink" data-pltdoc="x">super-new</a></span>, or
|
||
|
<span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-instantiate%29%29" class="RktStxLink" data-pltdoc="x">super-instantiate</a></span> invocation, after the explicit
|
||
|
arguments. If multiple initialization arguments are provided for
|
||
|
the same name, the first (if any) is used, and the unused
|
||
|
arguments are propagated to the superclass. (Note that converted
|
||
|
by-position arguments are always placed before explicit by-name
|
||
|
arguments.) The initialization procedure for the
|
||
|
<span class="RktSym"><a href="createclass.html#%28def._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._object~25%29%29" class="RktValLink" data-pltdoc="x">object%</a></span> class accepts zero initialization arguments; if
|
||
|
it receives any by-name initialization arguments, then
|
||
|
<span class="RktSym"><a href="objectutils.html#%28def._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._exn~3afail~3aobject%29%29" class="RktValLink" data-pltdoc="x">exn:fail:object</a></span> exception is raised.</p><p>If the end of initialization is reached for any class in the
|
||
|
hierarchy without invoking the superclass’s initialization, the
|
||
|
<span class="RktSym"><a href="objectutils.html#%28def._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._exn~3afail~3aobject%29%29" class="RktValLink" data-pltdoc="x">exn:fail:object</a></span> exception is raised. Also, if superclass initialization is
|
||
|
invoked more than once, the <span class="RktSym"><a href="objectutils.html#%28def._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._exn~3afail~3aobject%29%29" class="RktValLink" data-pltdoc="x">exn:fail:object</a></span> exception is raised.</p><p>Fields inherited from a superclass are not initialized until the
|
||
|
superclass’s initialization procedure is invoked. In contrast,
|
||
|
all methods are available for an object as soon as the object is
|
||
|
created; the overriding of methods is not affected by
|
||
|
initialization (unlike objects in C++).</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._racket/private/class-internal..rkt)._make-object))"></a><span title="Provided from: racket/class, racket | Package: base"><span class="RktSym"><a href="objcreation.html#%28def._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._make-object%29%29" class="RktValDef RktValLink" data-pltdoc="x">make-object</a></span></span><span class="hspace"> </span><span class="RktVar">class</span><span class="hspace"> </span><span class="RktVar">init-v</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="objectutils.html#%28def._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._object~3f%29%29" class="RktValLink" data-pltdoc="x">object?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">class</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="objectutils.html#%28def._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._class~3f%29%29" class="RktValLink" data-pltdoc="x">class?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">init-v</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29" class="RktValLink" data-pltdoc="x">any/c</a></span></td></tr></table></blockquote></div><div class="SIntrapara">Creates an instance of <span class="RktVar">class</span>. The <span class="RktVar">init-v</span>s are
|
||
|
passed as initialization arguments, bound to the initialization
|
||
|
variables of <span class="RktVar">class</span> for the newly created object as
|
||
|
described in <a href="createclass.html#%28part._clinitvars%29" data-pltdoc="x">Initialization Variables</a>. If <span class="RktVar">class</span> is not a
|
||
|
class, the <span class="RktSym"><a href="exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29" class="RktValLink" data-pltdoc="x">exn:fail:contract</a></span> exception is raised.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._racket/private/class-internal..rkt)._new))"></a><span title="Provided from: racket/class, racket | Package: base"><span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._new%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">new</a></span></span><span class="hspace"> </span><span class="RktVar">class-expr</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktVar">id</span><span class="hspace"> </span><span class="RktVar">by-name-expr</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">Creates an instance of the value of <span class="RktVar">class-expr</span> (which
|
||
|
must be a class), and the value of each <span class="RktVar">by-name-expr</span> is
|
||
|
provided as a by-name argument for the corresponding
|
||
|
<span class="RktVar">id</span>.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._racket/private/class-internal..rkt)._instantiate))"></a><span title="Provided from: racket/class, racket | Package: base"><span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._instantiate%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">instantiate</a></span></span><span class="hspace"> </span><span class="RktVar">class-expr</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktVar">by-pos-expr</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktVar">id</span><span class="hspace"> </span><span class="RktVar">by-name-expr</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">Creates an instance of the value of <span class="RktVar">class-expr</span> (which
|
||
|
must be a class), and the values of the <span class="RktVar">by-pos-expr</span>s are
|
||
|
provided as by-position initialization arguments. In addition,
|
||
|
the value of each <span class="RktVar">by-name-expr</span> is provided as a by-name
|
||
|
argument for the corresponding <span class="RktVar">id</span>.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><a name="(form._((lib._racket/private/class-internal..rkt)._super-make-object))"></a><span title="Provided from: racket/class, racket | Package: base"><span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-make-object%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">super-make-object</a></span></span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">Produces a procedure that takes by-position arguments an invokes
|
||
|
superclass initialization. See <a href="objcreation.html" data-pltdoc="x">Creating Objects</a> for more
|
||
|
information.</div></p><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._racket/private/class-internal..rkt)._super-instantiate))"></a><span title="Provided from: racket/class, racket | Package: base"><span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-instantiate%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">super-instantiate</a></span></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktVar">by-pos-expr</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktVar">id</span><span class="hspace"> </span><span class="RktVar">by-expr</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr></table></blockquote><p>Invokes superclass initialization with the specified by-position and
|
||
|
by-name arguments. See <a href="objcreation.html" data-pltdoc="x">Creating Objects</a> for more
|
||
|
information.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._racket/private/class-internal..rkt)._super-new))"></a><span title="Provided from: racket/class, racket | Package: base"><span class="RktSym"><a href="objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._super-new%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">super-new</a></span></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktVar">id</span><span class="hspace"> </span><span class="RktVar">by-name-expr</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">Invokes superclass initialization with the specified by-name
|
||
|
arguments. See <a href="objcreation.html" data-pltdoc="x">Creating Objects</a> for more information.</div></p><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, "8.6", "../");"/></form> <a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="createclass.html" title="backward to "6.2 Creating Classes"" data-pltdoc="x">← prev</a> <a href="mzlib_class.html" title="up to "6 Classes and Objects"" data-pltdoc="x">up</a> <a href="ivaraccess.html" title="forward to "6.4 Field and Method Access"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|