📄 ch12_01.htm
字号:
<html><head><title>Objects (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Objects"><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="ch11_03.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="ch12_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 12. Objects</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch12_01.htm">Brief Refresher on Object-Oriented Lingo</a><br><a href="ch12_02.htm">Perl's Object System</a><br><a href="ch12_03.htm">Method Invocation</a><br><a href="ch12_04.htm">Object Construction</a><br><a href="ch12_05.htm">Class Inheritance</a><br><a href="ch12_06.htm">Instance Destructors</a><br><a href="ch12_07.htm">Managing Instance Data</a><br><a href="ch12_08.htm">Managing Class Data</a><br><a href="ch12_09.htm">Summary</a><br></p></div><a name="INDEX-2336"></a><p>First of all, you need to understand packages and modules; see <a href="ch10_01.htm">Chapter 10, "Packages"</a>, and <a href="ch11_01.htm">Chapter 11, "Modules"</a>. You also need to know about referencesand data structures; see <a href="ch08_01.htm">Chapter 8, "References"</a> and<a href="ch09_01.htm">Chapter 9, "Data Structures"</a>. It's also helpful tounderstand a little about object-oriented programming (OOP), so in thenext section we'll give you a little course on OOL (object-orientedlingo).</p><h2 class="sect1">12.1. Brief Refresher on Object-Oriented Lingo</h2><a name="INDEX-2337"></a><a name="INDEX-2338"></a><p><a name="INDEX-2339"></a>An <em class="emphasis">object</em> is a data structure with a collectionof behaviors. We generally speak of the behaviors as acted out by theobject directly, sometimes to the point of anthropomorphizing theobject. For example, we might say that a rectangle "knows" how todisplay itself on the screen, or that it "knows" how to compute itsown area.</p><p><a name="INDEX-2340"></a><a name="INDEX-2341"></a><a name="INDEX-2342"></a><a name="INDEX-2343"></a><a name="INDEX-2344"></a><a name="INDEX-2345"></a>Every object gets its behaviors by virtue of being an<em class="emphasis">instance</em> of a <em class="emphasis">class</em>. Theclass defines <em class="emphasis">methods</em>: behaviors that apply tothe class and its instances. When the distinction matters, we referto methods that apply only to a particular object as<em class="emphasis">instance methods</em> and those that apply to theentire class as <em class="emphasis">class methods</em>. But this is onlya convention--to Perl, a method is just a method, distinguished onlyby the type of its first argument.</p><p>You can think of an instance method as some action performed by a particularobject, such as printing itself out, copying itself, oraltering one or more of its properties ("set this sword's name toAnduril"). Class methods might perform operations on many objectscollectively ("display all swords") or provide other operationsthat aren't dependent on any particular object ("from now on,whenever a new sword is forged, register its owner in thisdatabase"). Methods that generate instances (objects) of a classare called <em class="emphasis">constructor methods</em> ("create a sword with a gem-studdedhilt and a secret inscription"). These are usually class methods("make me a new sword") but can also be instance methods ("makea copy just like this sword here").</p><p><a name="INDEX-2346"></a><a name="INDEX-2347"></a><a name="INDEX-2348"></a><a name="INDEX-2349"></a><a name="INDEX-2350"></a><a name="INDEX-2351"></a>A class may <em class="emphasis">inherit</em> methods from <em class="emphasis">parent classes</em>, also knownas <em class="emphasis">base classes</em> or <em class="emphasis">superclasses</em>. If it does, it's known asa <em class="emphasis">derived class</em> or a <em class="emphasis">subclass</em>. (Confusing the issue further,some literature uses "base class" to mean a "most super" superclass.That's not what we mean by it.) Inheritance makes a new class thatbehaves just like an existing one but also allows for altered oradded behaviors not found in its parents. When you invoke a methodwhose definition is not found in the class, Perl automaticallyconsults the parent classes for a definition. For example, a swordclass might inherit its <tt class="literal">attack</tt> method from a generic bladeclass. Parents can themselves have parents, and Perl will searchthose classes as well when it needs to. The blade class might in turninherit its <tt class="literal">attack</tt> method from an even more generic weapon class.</p><p><a name="INDEX-2352"></a>When the <tt class="literal">attack</tt> method is invoked on an object, the resultingbehavior may depend on whether that object is a sword or an arrow.Perhaps there wouldn't be any difference at all, which would be thecase if both swords and arrows inherited their attacking behaviorfrom the generic weapon class. But if there were a difference inbehaviors, the method dispatch mechanism would always select the<tt class="literal">attack</tt> method suitable for the object in question. The usefulproperty of always selecting the most appropriate behavior for aparticular type of object is known as <em class="emphasis">polymorphism</em>. It's animportant form of not caring.</p><p>You have to care about the innards of your objects when you'reimplementing a class, but when you <em class="emphasis">use</em> a class, you should bethinking of its objects as black boxes. You can't see what's inside,you shouldn't need to know how it works, and you interact with the boxonly on its terms: via the methods provided by the class. Even if youknow what those methods do to the object, you should resist the urge tofiddle around yourself. It's like the remote control for yourtelevision set: even if you know what's going on inside it, youshouldn't monkey with its innards without good reason.</p><p><a name="INDEX-2353"></a><a name="INDEX-2354"></a><a name="INDEX-2355"></a>Perl lets you peer inside the object from outside the class when youneed to. But doing so breaks its <em class="emphasis">encapsulation</em>, the principle thatall access to an object should be through methods alone. Encapsulationdecouples the published interface (how an object should be used) fromthe implementation (how it actually works). Perl does not have anexplicit interface facility apart from this unwritten contract betweendesigner and user. Both parties are expected to exercise common senseand common decency: the user by relying only upon the documentedinterface, the designer by not breaking that interface.</p><p>Perl doesn't force a particular style of programming on you, and itdoesn't have the obsession with privacy that some other object-orientedlanguages do. Perl does have an obsession with freedom, however, andone of the freedoms you have as a Perl programmer is the right toselect as much or as little privacy as you like. In fact, Perl canhave stronger privacy in its classes and objects than C++. That is,Perl does not restrict you from anything, and in particular it doesn'trestrict you from restricting yourself, if you're into that kind ofthing. The sections <a href="ch12_05.htm#ch12-sect-pm">Section 12.5.5, "Private Methods"</a> and <a href="ch12_07.htm#ch12-sect-po">Section 12.7.5, "Using Closures for Private Objects"</a> later in this chapter demonstrate how you can increase your dosage ofdiscipline.</p><p>Admittedly, there's a lot more to objects than this, and a lot of waysto find out more about object-oriented design. But that's not our purpose here. So, on we go.</p><a name="INDEX-2599"></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="ch11_03.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="ch12_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">11.3. Overriding Built-in Functions</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">12.2. Perl's Object System</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 © 2001</a> O'Reilly & 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 + -