perltoot.html
来自「perl教程」· HTML 代码 · 共 771 行 · 第 1/5 页
HTML
771 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perltoot - Tom's object-oriented tutorial for perl</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perltoot - Tom's object-oriented tutorial for perl</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#creating_a_class">Creating a Class</a></li>
<ul>
<li><a href="#object_representation">Object Representation</a></li>
<li><a href="#class_interface">Class Interface</a></li>
<li><a href="#constructors_and_instance_methods">Constructors and Instance Methods</a></li>
<li><a href="#planning_for_the_future__better_constructors">Planning for the Future: Better Constructors</a></li>
<li><a href="#destructors">Destructors</a></li>
<li><a href="#other_object_methods">Other Object Methods</a></li>
</ul>
<li><a href="#class_data">Class Data</a></li>
<ul>
<li><a href="#accessing_class_data">Accessing Class Data</a></li>
<li><a href="#debugging_methods">Debugging Methods</a></li>
<li><a href="#class_destructors">Class Destructors</a></li>
<li><a href="#documenting_the_interface">Documenting the Interface</a></li>
</ul>
<li><a href="#aggregation">Aggregation</a></li>
<li><a href="#inheritance">Inheritance</a></li>
<ul>
<li><a href="#overridden_methods">Overridden Methods</a></li>
<li><a href="#multiple_inheritance">Multiple Inheritance</a></li>
<li><a href="#universal__the_root_of_all_objects">UNIVERSAL: The Root of All Objects</a></li>
</ul>
<li><a href="#alternate_object_representations">Alternate Object Representations</a></li>
<ul>
<li><a href="#arrays_as_objects">Arrays as Objects</a></li>
<li><a href="#closures_as_objects">Closures as Objects</a></li>
</ul>
<li><a href="#autoload__proxy_methods">AUTOLOAD: Proxy Methods</a></li>
<ul>
<li><a href="#autoloaded_data_methods">Autoloaded Data Methods</a></li>
<li><a href="#inherited_autoloaded_data_methods">Inherited Autoloaded Data Methods</a></li>
</ul>
<li><a href="#metaclassical_tools">Metaclassical Tools</a></li>
<ul>
<li><a href="#class__struct">Class::Struct</a></li>
<li><a href="#data_members_as_variables">Data Members as Variables</a></li>
</ul>
<li><a href="#notes">NOTES</a></li>
<ul>
<li><a href="#object_terminology">Object Terminology</a></li>
</ul>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#author_and_copyright">AUTHOR AND COPYRIGHT</a></li>
<li><a href="#copyright">COPYRIGHT</a></li>
<ul>
<li><a href="#acknowledgments">Acknowledgments</a></li>
</ul>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perltoot - Tom's object-oriented tutorial for perl</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Object-oriented programming is a big seller these days. Some managers
would rather have objects than sliced bread. Why is that? What's so
special about an object? Just what <em>is</em> an object anyway?</p>
<p>An object is nothing but a way of tucking away complex behaviours into
a neat little easy-to-use bundle. (This is what professors call
abstraction.) Smart people who have nothing to do but sit around for
weeks on end figuring out really hard problems make these nifty
objects that even regular people can use. (This is what professors call
software reuse.) Users (well, programmers) can play with this little
bundle all they want, but they aren't to open it up and mess with the
insides. Just like an expensive piece of hardware, the contract says
that you void the warranty if you muck with the cover. So don't do that.</p>
<p>The heart of objects is the class, a protected little private namespace
full of data and functions. A class is a set of related routines that
addresses some problem area. You can think of it as a user-defined type.
The Perl package mechanism, also used for more traditional modules,
is used for class modules as well. Objects "live" in a class, meaning
that they belong to some package.</p>
<p>More often than not, the class provides the user with little bundles.
These bundles are objects. They know whose class they belong to,
and how to behave. Users ask the class to do something, like "give
me an object." Or they can ask one of these objects to do something.
Asking a class to do something for you is calling a <em>class method</em>.
Asking an object to do something for you is calling an <em>object method</em>.
Asking either a class (usually) or an object (sometimes) to give you
back an object is calling a <em>constructor</em>, which is just a
kind of method.</p>
<p>That's all well and good, but how is an object different from any other
Perl data type? Just what is an object <em>really</em>; that is, what's its
fundamental type? The answer to the first question is easy. An object
is different from any other data type in Perl in one and only one way:
you may dereference it using not merely string or numeric subscripts
as with simple arrays and hashes, but with named subroutine calls.
In a word, with <em>methods</em>.</p>
<p>The answer to the second question is that it's a reference, and not just
any reference, mind you, but one whose referent has been <em>bless</em>()ed
into a particular class (read: package). What kind of reference? Well,
the answer to that one is a bit less concrete. That's because in Perl
the designer of the class can employ any sort of reference they'd like
as the underlying intrinsic data type. It could be a scalar, an array,
or a hash reference. It could even be a code reference. But because
of its inherent flexibility, an object is usually a hash reference.</p>
<p>
</p>
<hr />
<h1><a name="creating_a_class">Creating a Class</a></h1>
<p>Before you create a class, you need to decide what to name it. That's
because the class (package) name governs the name of the file used to
house it, just as with regular modules. Then, that class (package)
should provide one or more ways to generate objects. Finally, it should
provide mechanisms to allow users of its objects to indirectly manipulate
these objects from a distance.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?