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

📄 ch13_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Overloading (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Overloading"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch12_09.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part2.htm">Part 2: The Gory Details</a></td><td align="right" valign="top" width="172"><a href="ch13_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 13.  Overloading</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch13_01.htm">The overload Pragma</a><br><a href="ch13_02.htm">Overload Handlers</a><br><a href="ch13_03.htm">Overloadable Operators</a><br><a href="ch13_04.htm">The Copy Constructor (=)</a><br><a href="ch13_05.htm">When an Overload Handler Is Missing (nomethod and fallback)</a><br><a href="ch13_06.htm">Overloading Constants</a><br><a href="ch13_07.htm">Public Overload Functions</a><br><a href="ch13_08.htm">Inheritance and Overloading</a><br><a href="ch13_09.htm">Run-Time Overloading</a><br><a href="ch13_10.htm">Overloading Diagnostics</a><br></p></div><p><a name="INDEX-2600"></a><a name="INDEX-2601"></a>Objects are cool, but sometimes they're just a little <em class="emphasis">too</em> cool.Sometimes you would rather they behaved a little less like objects anda little more like regular data types.  But there's a problem:  objectsare referents represented by references, and references aren't terriblyuseful except as references.  You can't add references, or print them,or (usefully) apply many of Perl's built-in operators.  The only thingyou can do is dereference them.  So you find yourself writing manyexplicit method invocations, like this:<blockquote><pre class="programlisting">print $object-&gt;as_string;$new_object = $subject-&gt;add($object);</pre></blockquote><a name="INDEX-2602"></a>Such explicit dereferencing is in general a good thing; you shouldnever confuse your references with your referents, except when youwant to confuse them.  Now would be one of those times.  If you designyour class with <em class="emphasis">overloading</em>, you can pretend thereferences aren't there and simply say:<blockquote><pre class="programlisting">print $object;$new_object = $subject + $object;</pre></blockquote><a name="INDEX-2603"></a><a name="INDEX-2604"></a>When you overload one of Perl's built-in operators, you define how itbehaves when it's applied to objects of a particular class.  A numberof standard Perl modules use overloading, such as<tt class="literal">Math::BigInt</tt>, which lets you create<tt class="literal">Math::BigInt</tt> objects that behave just like regularintegers but have no size limits.  You can add them with<tt class="literal">+</tt>, divide them with <tt class="literal">/</tt>, comparethem with <tt class="literal">&lt;=&gt;</tt>, and print them with<tt class="literal">print</tt>.</p><p>Note that overloading is not the same as autoloading, which is loadinga missing function or method on demand.  Neither is it the same as overriding,which is one function or method masking another.  Overloading hides nothing;it adds meaning to an operation that would have been nonsense on a merereference.</p><h2 class="sect1">13.1. The overload Pragma</h2><p><a name="INDEX-2605"></a><a name="INDEX-2606"></a>The <tt class="literal">use overload</tt> pragma implements operatoroverloading.  You provide it with a key/value list of operators andtheir associated behaviors:<blockquote><pre class="programlisting">package MyClass;use overload   '+' =&gt; \&amp;myadd,            # coderef               '&lt;' =&gt; "less_than";        # named method             'abs' =&gt; sub { return @_ },  # anonymous subroutine</pre></blockquote>Now when you try to add two <tt class="literal">MyClass</tt> objects, the<tt class="literal">myadd</tt> subroutine will be called to create theresult.</p><p>When you try to compare two <tt class="literal">MyClass</tt> objects withthe <tt class="literal">&lt;</tt> operator, Perl notices that the behavioris specified as a string and interprets the string as a method nameand not simply as a subroutine name.  In the example above, the<tt class="literal">less_than</tt> method might be supplied by the<tt class="literal">MyClass</tt> package itself or inherited from a baseclass of <tt class="literal">MyClass</tt>, but the <tt class="literal">myadd</tt>subroutine must be supplied by the current package.  The anonymoussubroutine for <tt class="literal">abs</tt> supplies itself even moredirectly.  However these routines are supplied, we'll call them<em class="emphasis">handlers</em>.<a name="INDEX-2607"></a></p><p><a name="INDEX-2608"></a>For unary operators (those taking only one operand, like<tt class="literal">abs</tt>), the handler specified for the class isinvoked whenever the operator is applied to an object of that class.</p><p><a name="INDEX-2609"></a>For binary operators like <tt class="literal">+</tt> or<tt class="literal">&lt;</tt>, the handler is invoked whenever the firstoperand is an object of the class <em class="emphasis">or</em> when thesecond operand is an object of the class and the first operand has nooverloading behavior.  That's so you can say either:<blockquote><pre class="programlisting">$object + 6</pre></blockquote>or:<blockquote><pre class="programlisting">6 + $object</pre></blockquote>without having to worry about the order of operands.  (In the secondcase, the operands will be <em class="emphasis">swapped</em> when passed tothe handler). If our expression was:<blockquote><pre class="programlisting">$animal + $vegetable</pre></blockquote>and <tt class="literal">$animal</tt> and <tt class="literal">$vegetable</tt> wereobjects of different classes, both of which used overloading, then theoverloading behavior of <tt class="literal">$animal</tt> would be triggered.(We'll hope the animal likes vegetables.)</p><p><a name="INDEX-2610"></a>There is only one trinary (ternary) operator in Perl,<tt class="literal">?:</tt>, and you can't overload it.  Fortunately.</p><a name="INDEX-2652"></a><a name="INDEX-2653"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch12_09.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch13_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">12.9. Summary</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">13.2. Overload Handlers</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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