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

📄 ch13_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Overload Handlers (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="Overload Handlers"><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="ch13_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch13_01.htm">Chapter 13: Overloading</a></td><td align="right" valign="top" width="172"><a href="ch13_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">13.2. Overload Handlers</h2><p><a name="INDEX-2611"></a><a name="INDEX-2612"></a>When an overloaded operator is, er, operated, the correspondinghandler is invoked with three arguments.  The first two arguments arethe two operands.  If the operator only uses one operand, the secondargument is <tt class="literal">undef</tt>.</p><p><a name="INDEX-2613"></a><a name="INDEX-2614"></a>The third argument indicates whether the first two arguments wereswapped.  Even under the rules of normal arithmetic, some operations,like addition or multiplication, don't usually care about the order oftheir arguments, but others, like subtraction and division,do.<a href="#FOOTNOTE-1">[1]</a> Consider the difference between:<blockquote><pre class="programlisting">$object - 6</pre></blockquote>and:<blockquote><pre class="programlisting">6 - $object</pre></blockquote><a name="INDEX-2615"></a>If the first two arguments to a handler have been swapped, the thirdargument will be true.  Otherwise, the third argument will be false,in which case there is a finer distinction as well: if thehandler has been triggered by another handler involving assignment (asin <tt class="literal">+=</tt> using <tt class="literal">+</tt> to figure out howto add), then the third argument is not merely false, but<tt class="literal">undef</tt>.  This distinction enables someoptimizations.</p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1] Your overloaded objects are not required torespect the rules of normal arithmetic, of course, but it's usuallybest not to surprise people.  Oddly, many languages make the mistakeof overloading <tt class="literal">+</tt> with string concatenation, whichis not commutative and only vaguely additive.  For a differentapproach, see Perl.</p></blockquote><p>As an example, here is a class that lets you manipulate a boundedrange of numbers.  It overloads both <tt class="literal">+</tt> and<tt class="literal">-</tt> so that the result of adding or subtractingobjects constrains the values within the range 0 and 255:<blockquote><pre class="programlisting">package ClipByte;use overload '+' =&gt; \&amp;clip_add,             '-' =&gt; \&amp;clip_sub;sub new {    my $class = shift;    my $value = shift;    return bless \$value =&gt; $class;}sub clip_add {    my ($x, $y) = @_;    my ($value) = ref($x) ? $$x : $x;    $value     += ref($y) ? $$y : $y;    $value = 255 if $value &gt; 255;    $value =   0 if $value &lt; 0;    return bless \$value =&gt; ref($x);}sub clip_sub {    my ($x, $y, $swap) = @_;    my ($value) = (ref $x) ? $$x : $x;    $value     -= (ref $y) ? $$y : $y;    if ($swap) { $value = -$value }    $value = 255 if $value &gt; 255;    $value =   0 if $value &lt; 0;    return bless \$value =&gt; ref($x);}package main;$byte1 = ClipByte-&gt;new(200);$byte2 = ClipByte-&gt;new(100);$byte3 = $byte1 + $byte2;    # 255$byte4 = $byte1 - $byte2;    # 100$byte5 = 150 - $byte2;       # 50</pre></blockquote>You'll note that every function here is by necessity a constructor, soeach one takes care to <tt class="literal">bless</tt> its new object backinto the current class, whatever that is; we assume our class might beinherited.  We also assume that if <tt class="literal">$y</tt> is areference, it's a reference to an object of our own type.  Instead oftesting <tt class="literal">ref($y)</tt>, we could have called<tt class="literal">$y-&gt;isa("ClipByte")</tt> if we wanted to be morethorough (and run slower).</p><!-- 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="ch13_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">13.1. The overload Pragma</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.3. Overloadable Operators</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 + -