• <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>

            統計

            • 隨筆 - 50
            • 文章 - 42
            • 評論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 165529
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

            perl中bless的理解
          1. bless有兩個參數:對象的引用、類的名稱。
          2. 類的名稱是一個字符串,代表了類的類型信息,這是理解bless的關鍵。
          3. 所謂bless就是把 類型信息 賦予 實例變量。
            程序包括5個文件:
            person.pm :實現了person類
            dog.pm :實現了dog類
            bless.pl : 正確的使用bless
            bless.wrong.pl : 錯誤的使用bless
            bless.cc : 使用C++語言實現了與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"變為人
                   bless($object, "person");
                   $object->sleep();
                   $object->study();

                   # 再把"tom"變為狗
                   bless($object, "dog");
                   $object->sleep();
                   $object->bark();

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

            &main();

            # 程序運行時輸出:
            # 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();

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


            使用c++實現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"變為人
                   bless(o, person)->sleep();
                   bless(o, person)->study();

                   // 再把"tom"變為狗
                   bless(o, dog)->sleep();
                   bless(o, dog)->bark();

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

            // 程序運行時輸出:
            // 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) 評論(2)  編輯 收藏 引用

            評論

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

            不錯,DING一下!!
              回復  更多評論    

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

            兄弟理解得很清晰!贊一個!
              回復  更多評論    
            国产精品久久久久久影院| 久久久WWW免费人成精品| 囯产极品美女高潮无套久久久| 久久国产亚洲精品| 2022年国产精品久久久久| 91精品国产综合久久四虎久久无码一级| 国产成人久久777777| 久久久久久久久波多野高潮| 97久久久久人妻精品专区| 婷婷久久综合| 久久精品无码一区二区三区| 亚洲人AV永久一区二区三区久久| 久久偷看各类wc女厕嘘嘘| 久久99精品久久久久久9蜜桃| 一本久久a久久精品vr综合| 丁香久久婷婷国产午夜视频| 色综合久久中文字幕无码| 久久这里有精品视频| 国产精品久久免费| 97精品依人久久久大香线蕉97| 26uuu久久五月天| 91精品国产91久久综合| 亚洲色大成网站WWW久久九九| 久久亚洲AV永久无码精品| 久久香蕉国产线看观看乱码| 久久亚洲春色中文字幕久久久| 色播久久人人爽人人爽人人片aV| 99久久免费国产精品| av无码久久久久不卡免费网站| 亚洲欧美伊人久久综合一区二区| 武侠古典久久婷婷狼人伊人| 久久线看观看精品香蕉国产| 久久国产精品国产自线拍免费| 色偷偷久久一区二区三区| 久久婷婷五月综合色奶水99啪| 久久se精品一区二区影院| 国产福利电影一区二区三区,免费久久久久久久精 | 77777亚洲午夜久久多喷| 人人狠狠综合久久亚洲| 欧美日韩精品久久久免费观看| 久久996热精品xxxx|