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

📄 classes, objects, and ties.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: Classes, Objects, and Ties
Date: Wed, 29 Apr 2009 09:27:23 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/classesetc.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>Classes, Objects, and Ties</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3D"Packages, Libraries, and =
Modules"=20
href=3D"packagesetc.html" rel=3DPREVIOUS><LINK title=3D"Database Access" =

href=3D"dbaccess.html" rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
  <TBODY>
  <TR>
    <TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
  <TR>
    <TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/packagesetc.html">Prev</A=
></TD>
    <TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
    <TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/dbaccess.html">Next</A></=
TD></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DCLASSESETC>13. Classes, Objects, and =
Ties</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN704>Introduction</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT>$object =3D <FONT color=3D#00ffff>{};</FONT>                      =
 <FONT color=3D#bebebe># hash reference
</FONT><FONT color=3D#98fb98>bless</FONT><FONT =
color=3D#00ffff>(</FONT>$object, <FONT =
color=3D#00cd00>"Data::Encoder"</FONT><FONT color=3D#00ffff>);</FONT>    =
<FONT color=3D#bebebe># bless $object into Data::Encoder class
</FONT><FONT color=3D#98fb98>bless</FONT><FONT =
color=3D#00ffff>(</FONT>$object<FONT color=3D#00ffff>);</FONT>           =
          <FONT color=3D#bebebe># bless $object into current package
#-----------------------------
</FONT>$obj =3D <FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>3</FONT>,<FONT color=3D#cdcd00>5</FONT><FONT =
color=3D#00ffff>];</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#98fb98>ref</FONT><FONT =
color=3D#00ffff>(</FONT>$obj<FONT color=3D#00ffff>)</FONT>, <FONT =
color=3D#00cd00>" "</FONT>, $obj-&gt;<FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>]</FONT>, <FONT =
color=3D#00cd00>"\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>bless</FONT><FONT color=3D#00ffff>(</FONT>$obj, =
<FONT color=3D#00cd00>"Human::Cannibal"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#98fb98>ref</FONT><FONT =
color=3D#00ffff>(</FONT>$obj<FONT color=3D#00ffff>)</FONT>, <FONT =
color=3D#00cd00>" "</FONT>, $obj-&gt;<FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>]</FONT>, <FONT =
color=3D#00cd00>"\n"</FONT><FONT color=3D#00ffff>;</FONT>

ARRAY <FONT color=3D#cdcd00>5</FONT>

Human::Cannibal <FONT color=3D#cdcd00>5</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$obj-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>Stomach</FONT><FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#00cd00>"Empty"</FONT><FONT color=3D#00ffff>;</FONT>   <FONT =
color=3D#bebebe># directly accessing an object's contents
</FONT>$obj-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>NAME</FONT><FONT color=3D#00ffff>}</FONT>    =3D <FONT =
color=3D#00cd00>"Thag"</FONT><FONT color=3D#00ffff>;</FONT>        <FONT =
color=3D#bebebe># uppercase field name to make it stand out (optional)
#-----------------------------
</FONT>$encoded =3D $object-&gt;encode<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"data"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$encoded =3D Data::Encoder-&gt;encode<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"data"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>new</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$class</FONT> =
=3D <FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$self</FONT>  =
=3D <FONT color=3D#00ffff>{};</FONT>         <FONT color=3D#bebebe># =
allocate new hash for object
</FONT>    <FONT color=3D#98fb98>bless</FONT><FONT =
color=3D#00ffff>(</FONT>$self, $class<FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#ffa500>return</FONT> $self<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$object =3D Class-&gt;new<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$object =3D Class::new<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Class"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>class_only_method</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$class</FONT> =
=3D <FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"class method =
called on object"</FONT> <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#98fb98>ref</FONT> $class<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#bebebe># more code here
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>instance_only_method</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$self</FONT> =
=3D <FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"instance =
method called on class"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#98fb98>ref</FONT> $self<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#bebebe># more code here
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$lector =3D new Human::Cannibal<FONT color=3D#00ffff>;</FONT>
feed $lector <FONT color=3D#00cd00>"Zak"</FONT><FONT =
color=3D#00ffff>;</FONT>
move $lector <FONT color=3D#00cd00>"New York"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$lector =3D Human::Cannibal-&gt;
new<FONT color=3D#00ffff>();</FONT>

$lector-&gt;feed<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Zak"</FONT><FONT color=3D#00ffff>);</FONT>
$lector-&gt;move<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"New =
York"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>printf</FONT> STDERR <FONT =
color=3D#00cd00>"stuff here\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>move $obj-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>FIELD</FONT><FONT color=3D#00ffff>};</FONT>              =
   <FONT color=3D#bebebe># probably wrong
</FONT>move <FONT color=3D#cdad00>$ary</FONT><FONT =
color=3D#00ffff>[</FONT>$i<FONT color=3D#00ffff>];</FONT>                =
      <FONT color=3D#bebebe># probably wrong
#-----------------------------
</FONT>$obj-&gt;move-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>FIELD</FONT><FONT color=3D#00ffff>};</FONT>              =
  <FONT color=3D#bebebe># Surprise!
</FONT>$ary-&gt;move-&gt;<FONT color=3D#00ffff>[</FONT>$i<FONT =
color=3D#00ffff>];</FONT>                   <FONT color=3D#bebebe># =
Surprise!
#-----------------------------
</FONT>$obj-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>FIELD</FONT><FONT color=3D#00ffff>}</FONT>-&gt;
move<FONT color=3D#00ffff>()</FONT>
<FONT color=3D#00ffff>;</FONT>              <FONT color=3D#bebebe># =
Nope, you wish
</FONT><FONT color=3D#cdad00>$ary</FONT><FONT =
color=3D#00ffff>[</FONT>$i<FONT color=3D#00ffff>]</FONT>-&gt;
move<FONT color=3D#00ffff>;</FONT>
                     <FONT color=3D#bebebe># Nope, you wish
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN707>Constructing an Object</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>new</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$class</FONT> =
=3D <FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$self</FONT>  =
=3D <FONT color=3D#00ffff>{</FONT> <FONT color=3D#00ffff>};</FONT>
    <FONT color=3D#98fb98>bless</FONT><FONT =
color=3D#00ffff>(</FONT>$self, $class<FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#ffa500>return</FONT> $self<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>new</FONT> =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#98fb98>bless</FONT><FONT =
color=3D#00ffff>(</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#00ffff>}</FONT>, <FONT color=3D#ff7f50>shift</FONT> <FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>new</FONT> =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#98fb98>bless</FONT><FONT =
color=3D#00ffff>({})</FONT> <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>new</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$self</FONT> =
=3D <FONT color=3D#00ffff>{</FONT> <FONT color=3D#00ffff>};</FONT>  =
<FONT color=3D#bebebe># allocate anonymous hash
</FONT>    <FONT color=3D#98fb98>bless</FONT><FONT =
color=3D#00ffff>(</FONT>$self<FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#bebebe># init two sample attributes/data =
members/fields
</FONT>    $self-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>START</FONT><FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#98fb98>time</FONT><FONT color=3D#00ffff>();</FONT><FONT =
color=3D#43cd80>  </FONT>
    $self-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>AGE</FONT><FONT color=3D#00ffff>}</FONT>   =3D <FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> $self<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>new</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$classname</FONT>  =3D <FONT =
color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>         <FONT =
color=3D#bebebe># What class are we constructing?
</FONT>    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$self</FONT>      =3D <FONT color=3D#00ffff>{};</FONT>   =
          <FONT color=3D#bebebe># Allocate new memory
</FONT>    <FONT color=3D#98fb98>bless</FONT><FONT =
color=3D#00ffff>(</FONT>$self, $classname<FONT color=3D#00ffff>);</FONT> =
      <FONT color=3D#bebebe># Mark it of the right type
</FONT>    $self-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>START</FONT><FONT color=3D#00ffff>}</FONT>  =3D<FONT =
color=3D#43cd80> </FONT>
<FONT color=3D#98fb98>time</FONT><FONT color=3D#00ffff>();</FONT>
       <FONT color=3D#bebebe># init data fields
</FONT>    $self-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>AGE</FONT><FONT color=3D#00ffff>}</FONT>    =3D<FONT =
color=3D#43cd80> </FONT>
<FONT color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>;</FONT>

    <FONT color=3D#ffa500>return</FONT> $self<FONT =

⌨️ 快捷键说明

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