免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2623 | 回复: 10
打印 上一主题 下一主题

求解釋--關於重載 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-02-26 11:56 |只看该作者 |倒序浏览
《perl語言編程(第三版)》 第十三章 重載 裏面說的“自動生成” 的意思是說共用同一個重載處理器麽? 求高人指點 ^_^

论坛徽章:
0
2 [报告]
发表于 2013-02-27 12:22 |只看该作者
kelvenchi 发表于 2013-02-26 11:56
《perl語言編程(第三版)》 第十三章 重載 裏面說的“自動生成” 的意思是說共用同一個重載處理器麽? 求高 ...


重载与自动生成(autoload?)是两回事。

论坛徽章:
0
3 [报告]
发表于 2013-02-27 14:48 |只看该作者
回复 2# 兰花仙子

原来“自动生成”是AUTOLOAD ,这下我就明白了。感谢仙子  ^_^  


   

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
4 [报告]
发表于 2013-02-27 15:14 |只看该作者
回复 3# kelvenchi


    我觉得你理解错了,手头没书不过重载有个 Magic Autogeneration 概念(详情看 perl overload),指的是定义了 <=> 可以不用定义 > < <= >= != 等这些逻辑上相关的操作符

Magic Autogeneration
If a method for an operation is not found then Perl tries to autogenerate a substitute implementation from the operations that have been defined.
Note: the behaviour described in this section can be disabled by setting fallback to FALSE (see fallback).
In the following tables, numbers indicate priority. For example, the table below states that, if no implementation for '!' has been defined then Perl will implement it using 'bool' (that is, by inverting the value returned by the method for 'bool' ); if boolean conversion is also unimplemented then Perl will use '0+' or, failing that, '""' .
    operator | can be autogenerated from
             |
             | 0+   ""   bool   .   x
    =========|==========================
       0+    |       1     2
       ""    |  1          2
       bool  |  1    2
       int   |  1    2     3
       !     |  2    3     1
       qr    |  2    1     3
              .     |  2    1     3
       x     |  2    1     3
       .=    |  3    2     4    1
       x=    |  3    2     4        1
       <>    |  2    1     3
       -X    |  2    1     3
Note: The iterator ('<>' ) and file test ('-X' ) operators work as normal: if the operand is not a blessed glob or IO reference then it is converted to a string (using the method for '""' , '0+' , or 'bool' ) to be interpreted as a glob or filename.
    operator | can be autogenerated from
             |
             |  <   <=>   neg   -=    -
    =========|==========================
       neg   |                        1
       -=    |                        1
       --    |                   1    2
       abs   | a1    a2    b1        b2   

  •        <     |        1
           <=    |        1
           >     |        1
           >=    |        1
           ==    |        1
           !=    |        1
        * one from [a1, a2] and one from [b1, b2]
    Just as numeric comparisons can be autogenerated from the method for '<=>' , string comparisons can be autogenerated from that for 'cmp' :
         operators          |  can be autogenerated from
        ====================|===========================
         lt gt le ge eq ne  |  cmp
    Similarly, autogeneration for keys '+=' and '++' is analogous to '-=' and '--' above:
        operator | can be autogenerated from
                 |
                 |  +=    +
        =========|==========================
            +=   |        1
            ++   |   1    2
    And other assignment variations are analogous to '+=' and '-=' (and similar to '.=' and 'x=' above):
                  operator ||  *= /= %= **= <<= >>= &= ^= |=
        -------------------||--------------------------------
        autogenerated from ||  *  /  %  **  <<  >>  &  ^  |
    Note also that the copy constructor (key '=' ) may be autogenerated, but only for objects based on scalars. See Copy Constructor.
  • 论坛徽章:
    0
    5 [报告]
    发表于 2013-02-27 18:48 |只看该作者
    本帖最后由 kelvenchi 于 2013-02-27 20:43 编辑

    回复 4# zhlong8

    我就是不知道概念才这么问的。我以为仙子说的是对的呢。书上说的和你给的是一个意思,但是我还是有点不理解,我今天测试了下。我重载了 -(二元减号) 但是当我把-当负号使用的时候并没有自動生成 neg 这个操作符的重載處理器。所以我不明白,为什么会这样。

    代碼:

    #! /usr/bin/perl
    package AutoMake;
    use warnings;
    use strict;
    use overload '-' => \&mysub,
                 '""' => \&as_string;
                 #'neg' => \&nn;

    sub new {
        my $class = shift;
        my $value = shift;
        bless \$value => $class;
    }

    sub mysub {
        my ($x,$y) = @_;
        my $value = (ref $x) ? $$x : $x;
        $value += (ref $y) ? $$y : $y;
        return bless \$value => ref ($x);
    }

    sub as_string {
        my $self = shift;
        return $$self;
    }

    sub nn {
        my $self = shift;
        return -$$self;
    }

    my $kelven = AutoMake->new(100);
    my $kelven2 = AutoMake->new(200);
    my $kk = -$kelven;
    my $ks = $kelven - $kelven2;

    print $kk,"\n";
    print "$kelven\n";
    print $ks,"\n";

    輸出結果:

    root@kelven:~/Perl-Test # perl automake_ol.pl
    100
    100
    300
    root@kelven:~/Perl-Test #

    我以爲會輸出:
    -100
    100
    300

    我開始以爲是共用同一個處理器,所以我把 mysub 那個子例程裏的 “+=” 改成 “-=” 以爲這樣就可以了。但是輸出結果是:
    100
    100
    -100

    真心糊塗了,求高手解釋 ^_^  不甚感激!

    论坛徽章:
    0
    6 [报告]
    发表于 2013-02-27 21:26 |只看该作者
    本帖最后由 kelvenchi 于 2013-02-27 21:56 编辑

    回复 5# kelvenchi

    我明白了,實際上我的理解一部分是對的。它就是調用了某一個處理器,- 當作負號使用的時候 第一個參數其實是 undef 第二個參數是 $kelven 這種情況下,perl在調用重載處理器的時候會交換兩個參數的位置,這樣 就變成了 $kelven - 0 所以返回的結果是100。我試了下 把代碼中: $kk = -$kelven 改成 $kk = 1 - $kelven 會打印出: 101 這個數值;把 += 改成 -= 打印出 99 這個數值。

    在大駱駝書中是這樣描述的:“如果重載了 - 而沒有重載 neg ,然後試圖使用一個一元負號,Perl會為你模擬一個neg處理器。這就是所謂的自動生成,就是説某些操作符可以合理地從其他操作符中歸納出來(以該重載操作符將和普通操作符一樣的關係為前提)”。一開始還不理解,現在理解了。

    如果沒有指定 !的處理器,那麽它可以用 bool ,"" ,0+ 處理器“自動生成”。每一重類別中可用於重載中“自動生成”的處理器有些不同

    论坛徽章:
    0
    7 [报告]
    发表于 2013-02-28 09:44 |只看该作者
    提示: 作者被禁止或删除 内容自动屏蔽

    论坛徽章:
    0
    8 [报告]
    发表于 2013-02-28 10:43 |只看该作者
    回复 7# 芙蓉女侠

    我是大陸的


       

    论坛徽章:
    0
    9 [报告]
    发表于 2013-02-28 10:49 |只看该作者
    提示: 作者被禁止或删除 内容自动屏蔽

    论坛徽章:
    0
    10 [报告]
    发表于 2013-02-28 14:50 |只看该作者
    我手寫的都是正體中文
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP