📄 ch07_04.htm
字号:
<html><head><title>Object Syntax (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch07_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">7.4. Object Syntax</h2><p><a name="INDEX-1259" /><a name="INDEX-1260" />Perl uses two forms ofsyntax for invoking methods on objects. For both types of syntax, theobject reference or class name is given as the first argument. Amethod that takes a class name is called a <em class="emphasis">classmethod</em>, and one that takes an object reference is calledan <em class="emphasis">instance method</em>.</p><p><a name="INDEX-1261" />Class methodsprovide functionality for the entire class, not just for a singleobject that belongs to the class. Class methods expect a class nameas their first argument. Following this explanation, a constructor isan example of a class method:</p><blockquote><pre class="code">sub new { my $self = {}; bless $self; return $self;}</pre></blockquote><p><a name="INDEX-1262" />On theother hand, an instance method expects an object reference as itsfirst argument. An instance method will shift the first argument anduse this argument as a reference:</p><blockquote><pre class="code">sub instance_method { my $self = shift; my($one, $two, $three) = @_; # do stuff}</pre></blockquote><p>Here is an example of a constructor creating a new object andreturning a reference:</p><blockquote><pre class="code">$tri = Triangle::Right->new(side1 => 3, side2 => 4);</pre></blockquote><p>This example creates a new right-triangle object and references itwith <tt class="literal">$tri</tt>. The parameters are given as ahash-style list. This is common for constructors, since they setinitial parameters for an object that is probably just a hash. Nowthat we have an object, we can invoke a method on it. SupposeTriangle::Right defines a method, <tt class="literal">hypot</tt>, thatreturns the length of the hypotenuse for a given right-triangleobject. It would be used like this:</p><blockquote><pre class="code">$h = $tri->hypot;print "The hypotenuse is: $h.\n";</pre></blockquote><p>In this particular example, there happens to be no additionalarguments to the <tt class="literal">hypot</tt> method, but there couldhave been.</p><p><a name="INDEX-1263" />Withthe arrow (<tt class="literal">-></tt>) notation, the left side of thearrow must be an object reference or a class name, while the rightside of the arrow must be a method defined for that object. Anyarguments must follow the method inside of parentheses. For example:</p><blockquote><pre class="code">$obj->method(<em class="replaceable"><tt>args</tt></em>)CLASS->method(<em class="replaceable"><tt>args</tt></em>)</pre></blockquote><p>You have to use parentheses because this form can'tbe used as a list operator, although the first type of method syntaxcan.</p><p>The examples given above would look like this using the arrow syntax: </p><blockquote><pre class="code">$tri = Triangle::Right->new(side1 => 3, side2 => 4);$h = $tri->hypot( );print "The hypotenuse is: $h.\n";</pre></blockquote><p>The arrow syntax provides a helpful visual relationship between theobject and its method, but both forms of syntax do the same thing.Precedence for the arrow syntax is left-to-right, exactly the same asfor the dereferencing operator. This allows you to chain togetherobjects and methods if you want to simplify things. You just have tomake sure you have an object to the left of the arrow and a method tothe right:</p><blockquote><pre class="code">%sides = (side1 => 3, side2 => 4);$h = Triangle::Right->new(%sides)->hypot( );print "The hypotenuse is: $h.\n";</pre></blockquote><p>In this example, you never assign a variable name to theright-triangle object; the reference is passed directly to the<tt class="literal">hypot</tt> method.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch07_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">7.3. Object-Oriented Perl</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8. Standard Modules</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & 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 + -