• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            統(tǒng)計(jì)

            • 隨筆 - 50
            • 文章 - 42
            • 評(píng)論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 165546
            • 排名 - 159

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            perl中bless的理解
          1. bless有兩個(gè)參數(shù):對(duì)象的引用、類的名稱。
          2. 類的名稱是一個(gè)字符串,代表了類的類型信息,這是理解bless的關(guān)鍵。
          3. 所謂bless就是把 類型信息 賦予 實(shí)例變量。
            程序包括5個(gè)文件:
            person.pm :實(shí)現(xiàn)了person類
            dog.pm :實(shí)現(xiàn)了dog類
            bless.pl : 正確的使用bless
            bless.wrong.pl : 錯(cuò)誤的使用bless
            bless.cc : 使用C++語言實(shí)現(xiàn)了與bless.pl相同功能的代碼


            person.pm
            CODE:
            #!/usr/bin/perl -w
            package person;
            use strict;

            sub sleep() {
                   my ($self) = @_;
                   my $name = $self->{"name"};

                   print("$name is person, he is sleeping\n");
            }

            sub study() {
                   my ($self) = @_;
                   my $name = $self->{"name"};

                   print("$name is person, he is studying\n");
            }
            return 1;


            dog.pm
            CODE:
            #!/usr/bin/perl -w
            package dog;
            use strict;

            sub sleep() {
                   my ($self) = @_;
                   my $name = $self->{"name"};

                   print("$name is dog, he is sleeping\n");
            }

            sub bark() {
                   my ($self) = @_;
                   my $name = $self->{"name"};

                   print("$name is dog, he is barking\n");
            }

            return 1;


            bless.pl
            CODE:
            #!/usr/bin/perl =w
            use strict;
            use person;
            use dog;

            sub main()
            {
                   my $object = {"name" => "tom"};

                   # 先把"tom"變?yōu)槿?br>       bless($object, "person");
                   $object->sleep();
                   $object->study();

                   # 再把"tom"變?yōu)楣?br>       bless($object, "dog");
                   $object->sleep();
                   $object->bark();

                   # 最后,再把"tom"變回人
                   bless($object, "person");
                   $object->sleep();
                   $object->study();
            }

            &main();

            # 程序運(yùn)行時(shí)輸出:
            # tom is person, he is sleeping
            # tom is person, he is studying
            # tom is dog, he is sleeping
            # tom is dog, he is barking
            # tom is person, he is sleeping
            # tom is person, he is studying


            bless.wrong.pl
            CODE:
            #!/usr/bin/perl =w
            use strict;
            use person;
            use dog;

            sub main()
            {
                   my $object = {"name" => "tom"};

                   # 沒有把類型信息和$object綁定,因此無法獲知$object有sleep方法
                   $object->sleep();
                   $object->study();
            }

            &main();

            # 程序運(yùn)行輸出為:
            # Can't call method "sleep" on unblessed reference at bless.wrong.pl line 10.


            使用c++實(shí)現(xiàn)bless的功能

            c中的代碼
            CODE:
            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>

            struct object {
                   char name[16];
            };

            struct person {
                   char name[16];

                   void sleep() { printf("%s is person, he is sleeping\n", this->name); }
                   void study() { printf("%s is person, he is studying\n", this->name); }
            };

            struct dog {
                   char name[16];

                   void sleep() { printf("%s is dog, he is sleeping\n", this->name); }
                   void bark() { printf("%s is dog, he is barking\n", this->name); }
            };

            #define bless(object, type) ((type*) object)

            int main()
            {
                   struct object * o = (struct object *) malloc(sizeof(struct object));
                   strcpy(o->name, "tom");

                   // 先把"tom"變?yōu)槿?br>       bless(o, person)->sleep();
                   bless(o, person)->study();

                   // 再把"tom"變?yōu)楣?br>       bless(o, dog)->sleep();
                   bless(o, dog)->bark();

                   // 最后,再把"tom"變回人
                   bless(o, person)->sleep();
                   bless(o, person)->study();
                   return 0;
            }

            // 程序運(yùn)行時(shí)輸出:
            // tom is person, he is sleeping
            // tom is person, he is studying
            // tom is dog, he is sleeping
            // tom is dog, he is barking
            // tom is person, he is sleeping
            // tom is person, he is studying
          4. posted on 2009-03-04 18:12 pear_li 閱讀(1870) 評(píng)論(2)  編輯 收藏 引用

            評(píng)論

            # re: perl中bless的理解[未登錄] 2010-01-06 23:55 T.S liu

            不錯(cuò),DING一下!!

            # re: perl中bless的理解 2011-08-17 00:09 gg

            兄弟理解得很清晰!贊一個(gè)!

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久国产一区二区| 日本亚洲色大成网站WWW久久| 久久久久国产精品嫩草影院| 久久午夜无码鲁丝片秋霞| 亚洲中文字幕无码久久综合网| 久久久国产精品亚洲一区| AA级片免费看视频久久| 久久国产免费直播| 色综合久久88色综合天天| 亚洲成av人片不卡无码久久| 久久精品亚洲精品国产色婷 | 少妇久久久久久被弄到高潮| 亚洲中文精品久久久久久不卡| 9久久9久久精品| 人妻少妇精品久久| 97超级碰碰碰碰久久久久| 欧美喷潮久久久XXXXx| 午夜精品久久久久久影视777| 国产精品禁18久久久夂久| 久久午夜夜伦鲁鲁片免费无码影视| 国产激情久久久久影院老熟女免费 | 久久A级毛片免费观看| 久久久精品视频免费观看| 亚洲国产精久久久久久久| 久久国产精品无码HDAV| 伊人色综合久久天天人手人婷| 看全色黄大色大片免费久久久| 国产激情久久久久影院老熟女免费 | 亚洲国产精品久久久久婷婷老年| 色妞色综合久久夜夜| 久久国产免费直播| 亚洲va中文字幕无码久久不卡| 色综合久久中文字幕综合网| 亚洲国产日韩欧美久久| 午夜精品久久影院蜜桃| 久久亚洲精品国产亚洲老地址| 国产精品成人99久久久久91gav| 99久久精品免费看国产免费| 狠狠久久综合伊人不卡| 久久久久无码国产精品不卡| 久久青青草原精品国产不卡|