⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch13_07.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 13.6. Cloning Objects (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:42:22Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch13_01.htm"TITLE="13. Classes, Objects, and Ties"><LINKREL="prev"HREF="ch13_06.htm"TITLE="13.5. Using Classes as Structs"><LINKREL="next"HREF="ch13_08.htm"TITLE="13.7. Calling Methods Indirectly"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch13_06.htm"TITLE="13.5. Using Classes as Structs"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 13.5. Using Classes as Structs"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch13_01.htm"TITLE="13. Classes, Objects, and Ties"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch13_08.htm"TITLE="13.7. Calling Methods Indirectly"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13.7. Calling Methods Indirectly"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch13-36837">13.6. Cloning Objects</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-927">Problem<ACLASS="indexterm"NAME="ch13-idx-1000004533-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004533-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004533-2"></A><ACLASS="indexterm"NAME="ch13-idx-1000004533-3"></A><ACLASS="indexterm"NAME="ch13-idx-1000004533-4"></A></A></H3><PCLASS="para">You want to write a constructor method that might be called on an existing object.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-933">Solution</A></H3><PCLASS="para">Start your constructor like this:</P><PRECLASS="programlisting">my $proto  = shift;my $class  = ref($proto) || $proto;my $parent = ref($proto) &amp;&amp; $proto;</PRE><PCLASS="para">The <CODECLASS="literal">$class</CODE> variable will contain the class to bless into, and the <CODECLASS="literal">$parent</CODE> variable will either be false, or else the object you're cloning.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-947">Discussion</A></H3><PCLASS="para">Sometimes you need another object of the same type as the current one. You could do this:</P><PRECLASS="programlisting">$ob1 = SomeClass-&gt;<CODECLASS="literal">new()</CODE>;# later on$ob2 = (ref $ob1)-&gt;<CODECLASS="literal">new();</CODE></PRE><PCLASS="para">but that's not very clear. It's clearer to have a single constructor that can be called on the class or an existing object. As a class method, it should return a new object with the default initialization. As an instance method, it should return a new object initialized from the object it was called on:</P><PRECLASS="programlisting">$ob1 = Widget-&gt;new();$ob2 = $ob1-&gt;new();</PRE><PCLASS="para">Here's a version of <CODECLASS="literal">new</CODE> that takes this into consideration:</P><PRECLASS="programlisting">sub new {    my $proto  = shift;    my $class  = ref($proto) || $proto;    my $parent = ref($proto) &amp;&amp; $proto;    my $self;    # check whether we're shadowing a new from @ISA    if (@ISA &amp;&amp; $proto-&gt;SUPER::can('new') ) {        $self = $proto-&gt;SUPER::new(@_);    } else {         $self = {};        bless ($self, $proto);    }    bless($self, $class);    $self-&gt;{PARENT}  = $parent;    $self-&gt;{START}   = time();   # init data fields    $self-&gt;{AGE}     = 0;    return $self;} </PRE><PCLASS="para">Initializing doesn't have to mean simply copying values from the parent. If you're writing a linked list or binary tree class, your constructor can return a new object linked into the list or tree, when called as an instance method.<ACLASS="indexterm"NAME="ch13-idx-1000004535-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004535-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004535-2"></A><ACLASS="indexterm"NAME="ch13-idx-1000004535-3"></A><ACLASS="indexterm"NAME="ch13-idx-1000004535-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1001">See Also</A></H3><PCLASS="para"><ICLASS="filename">perlobj </I>(1) and <ACLASS="olink"HREF="../prog/ch05_01.htm">Chapter 5</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; <ACLASS="xref"HREF="ch13_02.htm"TITLE="Constructing an Object">Recipe 13.1</A>; <ACLASS="xref"HREF="ch13_10.htm"TITLE="Writing an Inheritable Class">Recipe 13.9</A>; <ACLASS="xref"HREF="ch13_14.htm"TITLE="Coping with Circular Data Structures">Recipe 13.13</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch13_06.htm"TITLE="13.5. Using Classes as Structs"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 13.5. Using Classes as Structs"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch13_08.htm"TITLE="13.7. Calling Methods Indirectly"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13.7. Calling Methods Indirectly"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">13.5. Using Classes as Structs</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">13.7. Calling Methods Indirectly</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -