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

📄 classes, objects, and ties.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$obj-&gt;can<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>'method_name'</FONT><FONT =
color=3D#00ffff>)</FONT>-&gt;<FONT color=3D#00ffff>(</FONT>$obj_target, =
<FONT color=3D#cdad00>@arguments</FONT><FONT color=3D#00ffff>)</FONT>
   <FONT color=3D#ffa500>if</FONT> $obj_target-&gt;isa<FONT =
color=3D#00ffff>(</FONT> <FONT color=3D#98fb98>ref</FONT> $obj <FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN728>Determining Subclass =
Membership</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>$obj-&gt;isa<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"HTTP::Message"</FONT><FONT color=3D#00ffff>);</FONT>    =
              <FONT color=3D#bebebe># as object method
</FONT>HTTP::Response-&gt;isa<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"HTTP::Message"</FONT><FONT color=3D#00ffff>);</FONT>    =
   <FONT color=3D#bebebe># as class method
</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$obj-&gt;can<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"method_name"</FONT><FONT color=3D#00ffff>))</FONT> =
<FONT color=3D#00ffff>{</FONT> .... <FONT color=3D#00ffff>}</FONT>       =
<FONT color=3D#bebebe># check method validity
#-----------------------------
</FONT>$has_io =3D $fd-&gt;isa<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"IO::Handle"</FONT><FONT color=3D#00ffff>);</FONT>
$itza_handle =3D IO::Socket-&gt;isa<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"IO::Handle"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$his_<FONT color=3D#ff7f50>print</FONT>_method =3D =
$obj-&gt;can<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>'as_string'</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>Some_Module-&gt;VERSION<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>3.0</FONT><FONT color=3D#00ffff>);</FONT>
$his_vers =3D $obj-&gt;
VERSION<FONT color=3D#00ffff>()</FONT>
<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Some_Module</FONT> <FONT color=3D#cdcd00>3.0</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>vars</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>$VERSION<FONT color=3D#00ffff>);</FONT>
$VERSION =3D <FONT color=3D#00cd00>'1.01'</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN731>Writing an Inheritable Class</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>package</FONT> <FONT =
color=3D#b2dfee>Person</FONT><FONT color=3D#00ffff>;</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#ffa500>return</FONT> <FONT =
color=3D#98fb98>bless</FONT> $self, $class<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>name</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>
    $self-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>NAME</FONT><FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#ff7f50>shift</FONT> <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> $self-&gt;<FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>NAME</FONT><FONT =
color=3D#00ffff>};</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>age</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>
    $self-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>AGE</FONT><FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#ff7f50>shift</FONT> <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> $self-&gt;<FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>AGE</FONT><FONT =
color=3D#00ffff>};</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Person</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$dude</FONT> =3D =
Person-&gt;
new<FONT color=3D#00ffff>()</FONT>
<FONT color=3D#00ffff>;</FONT>
$dude-&gt;name<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Jason"</FONT><FONT color=3D#00ffff>);</FONT>
$dude-&gt;age<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>23</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>printf</FONT> <FONT color=3D#00cd00>"</FONT><FONT =
color=3D#cdcd00>%s</FONT><FONT color=3D#00cd00> is age </FONT><FONT =
color=3D#cdcd00>%d</FONT><FONT color=3D#00cd00>.\n"</FONT>, =
$dude-&gt;name, $dude-&gt;age<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>package</FONT> <FONT =
color=3D#b2dfee>Employee</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Person</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>@ISA</FONT> =3D <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Person"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Employee</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$empl</FONT> =3D =
Employee-&gt;
new<FONT color=3D#00ffff>()</FONT>
<FONT color=3D#00ffff>;</FONT>
$empl-&gt;name<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Jason"</FONT><FONT color=3D#00ffff>);</FONT>
$empl-&gt;age<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>23</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>printf</FONT> <FONT color=3D#00cd00>"</FONT><FONT =
color=3D#cdcd00>%s</FONT><FONT color=3D#00cd00> is age </FONT><FONT =
color=3D#cdcd00>%d</FONT><FONT color=3D#00cd00>.\n"</FONT>, =
$empl-&gt;name, $empl-&gt;age<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$him =3D Person::
new<FONT color=3D#00ffff>()</FONT>
<FONT color=3D#00ffff>;</FONT>               <FONT color=3D#bebebe># =
WRONG
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN734>Accessing Overridden Methods</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>meth</FONT> <FONT color=3D#00ffff>{</FONT><FONT =
color=3D#43cd80> </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>
    $self-&gt;SUPER::
meth<FONT color=3D#00ffff>()</FONT>
<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$self-&gt;
meth<FONT color=3D#00ffff>();</FONT>
                <FONT color=3D#bebebe># Call wherever first meth is =
found
</FONT>$self-&gt;Where::
meth<FONT color=3D#00ffff>();</FONT>
         <FONT color=3D#bebebe># Start looking in package "Where"
</FONT>$self-&gt;SUPER::
meth<FONT color=3D#00ffff>();</FONT><FONT color=3D#43cd80> </FONT>
        <FONT color=3D#bebebe># Call overridden version
#-----------------------------
</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 $classname-&gt;SUPER::new<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@_</FONT><FONT =
color=3D#00ffff>);</FONT>
    $self-&gt;_init<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#ffa500>return</FONT> $self<FONT =
color=3D#00ffff>;</FONT>                   <FONT color=3D#bebebe># And =
give it back
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>_init</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>
    $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#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#cdcd00>0</FONT><FONT color=3D#00ffff>;</FONT>
    $self-&gt;<FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>EXTRA</FONT><FONT color=3D#00ffff>}</FONT>   =3D <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdad00>@_</FONT> <FONT =
color=3D#00ffff>};</FONT>   <FONT color=3D#bebebe># anything extra
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$obj =3D Widget-&gt;new<FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#00cd00>haircolor</FONT> =3D&gt; red, <FONT =
color=3D#00cd00>freckles</FONT> =3D&gt; <FONT color=3D#cdcd00>121</FONT> =
<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$self</FONT> =3D <FONT color=3D#98fb98>bless</FONT> =
<FONT color=3D#00ffff>{}</FONT>, $class<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>for</FONT> <FONT color=3D#ffa500>my</FONT> $class =
<FONT color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@ISA</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$meth</FONT> =
=3D $class . <FONT color=3D#00cd00>"::_init"</FONT><FONT =
color=3D#00ffff>;</FONT>
    $self-&gt;$meth<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#ffa500>if</FONT> $class-&gt;can<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"_init"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN737>Generating Attribute Methods Using=20
AUTOLOAD</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>package</FONT> <FONT =
color=3D#b2dfee>Person</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>strict</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>Carp</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>vars</FONT> <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>$AUTOLOAD <FONT =
color=3D#cdcd00>%ok_field</FONT><FONT color=3D#00ffff>);</FONT>

<FO

⌨️ 快捷键说明

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