免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 60430 | 回复: 81

我理解bless了,哈哈,给大家个例子。 [复制链接]

论坛徽章:
0
发表于 2007-08-24 14:06 |显示全部楼层

程序说明

网上的很多教程都没有把bless讲清楚,我通过摸索和实验,终于明白bless是什么意思了,简单的讲:
  • bless有两个参数:对象的引用、类的名称。
  • 类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键。
  • 所谓bless就是把 类型信息 赋予 实例变量。


程序包括5个文件:
person.pm :实现了person类
dog.pm :实现了dog类
bless.pl : 正确的使用bless
bless.wrong.pl : 错误的使用bless
bless.cc : 使用C++语言实现了与bless.pl相同功能的代码


person.pm


  1. #!/usr/bin/perl -w
  2. package person;
  3. use strict;

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

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

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

  12.         print("$name is person, he is studying\n");
  13. }
  14. return 1;
复制代码



dog.pm


  1. #!/usr/bin/perl -w
  2. package dog;
  3. use strict;

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

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

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

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

  14. return 1;
复制代码



bless.pl


  1. #!/usr/bin/perl =w
  2. use strict;
  3. use person;
  4. use dog;

  5. sub main()
  6. {
  7.         my $object = {"name" => "tom"};

  8.         # 先把"tom"变为人
  9.         bless($object, "person");
  10.         $object->sleep();
  11.         $object->study();

  12.         # 再把"tom"变为狗
  13.         bless($object, "dog");
  14.         $object->sleep();
  15.         $object->bark();

  16.         # 最后,再把"tom"变回人
  17.         bless($object, "person");
  18.         $object->sleep();
  19.         $object->study();
  20. }

  21. &main();

  22. # 程序运行时输出:
  23. # tom is person, he is sleeping
  24. # tom is person, he is studying
  25. # tom is dog, he is sleeping
  26. # tom is dog, he is barking
  27. # tom is person, he is sleeping
  28. # tom is person, he is studying
复制代码



bless.wrong.pl


  1. #!/usr/bin/perl =w
  2. use strict;
  3. use person;
  4. use dog;

  5. sub main()
  6. {
  7.         my $object = {"name" => "tom"};

  8.         # 没有把类型信息和$object绑定,因此无法获知$object有sleep方法
  9.         $object->sleep();
  10.         $object->study();
  11. }

  12. &main();

  13. # 程序运行输出为:
  14. # Can't call method "sleep" on unblessed reference at bless.wrong.pl line 10.
复制代码



使用c++实现bless的功能

c中的代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct object {
  5.         char name[16];
  6. };

  7. struct person {
  8.         char name[16];

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

  12. struct dog {
  13.         char name[16];

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

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

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

  22.         // 先把"tom"变为人
  23.         bless(o, person)->sleep();
  24.         bless(o, person)->study();

  25.         // 再把"tom"变为狗
  26.         bless(o, dog)->sleep();
  27.         bless(o, dog)->bark();

  28.         // 最后,再把"tom"变回人
  29.         bless(o, person)->sleep();
  30.         bless(o, person)->study();
  31.         return 0;
  32. }

  33. // 程序运行时输出:
  34. // tom is person, he is sleeping
  35. // tom is person, he is studying
  36. // tom is dog, he is sleeping
  37. // tom is dog, he is barking
  38. // tom is person, he is sleeping
  39. // tom is person, he is studying
复制代码

关键的地方就是把对象o的类型转变为person类型和dog类型

[ 本帖最后由 DennisRitchie 于 2007-8-24 14:09 编辑 ]

论坛徽章:
0
发表于 2007-08-24 14:34 |显示全部楼层
."".    ."",
     |  |   /  /
     |  |  /  /
     |  | /  /
     |  |/  ;-._
     }  ` _/  / ;         恭喜!!!
     |  /` ) /  /
     | /  /_/\_/\
     |/  /      |
     (  ' \ '-  |
      \    `.  /
       |      |
        |      |

论坛徽章:
0
发表于 2007-08-24 14:40 |显示全部楼层
原帖由 royalzhang 于 2007-8-24 14:34 发表
."".    ."",
     |  |   /  /
     |  |  /  /
     |  | /  /
     |  |/  ;-._
     }  ` _/  / ;         恭喜!!!
     |  /` ) /  /
     | /  /_/\_/\
     |/  /      |
     (  ' \ '-   ...


谢谢,我已经跨入OO编程了,呵呵。

论坛徽章:
0
发表于 2007-08-24 14:42 |显示全部楼层
厉害,学习了.

论坛徽章:
0
发表于 2007-08-25 00:17 |显示全部楼层
楼主适合写程序啊,一上来就问的人得好好学学,其实知识就在身边,看你关心不关心而已。
接去,Perl有意思的东西多着呢,特别是写“严格”程序的人,保证越琢磨越有味,不过要用来写商品软件就要先想好了,毕竟……。
pengchy 该用户已被删除
发表于 2007-08-25 12:46 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
发表于 2007-08-27 02:02 |显示全部楼层
原帖由 pengchy 于 2007-8-25 12:46 发表
还以为bless只能用在.pm文件中呢!

perl的bless关键字对初学者是一个难点,部分原因就是bless的"英文含义"与它perl中的含义毫无关联,可以说是“驴头不对马嘴”!

论坛徽章:
0
发表于 2007-08-27 11:15 |显示全部楼层
原帖由 DennisRitchie 于 2007-8-27 02:02 发表

perl的bless关键字对初学者是一个难点,部分原因就是bless的"英文含义"与它perl中的含义毫无关联,可以说是“驴头不对马嘴”!


拜托放轻松一些,编程也不要总是正襟危坐好不好?
怎么能说bless与它在perl中的含义毫无关联呢?
一个死气沉沉的引用,经过赐福,就神气活现地具有了某种灵性,这个词难道用的还不好吗?

论坛徽章:
0
发表于 2007-08-28 00:42 |显示全部楼层
原帖由 福瑞哈哥 于 2007-8-27 11:15 发表


拜托放轻松一些,编程也不要总是正襟危坐好不好?
怎么能说bless与它在perl中的含义毫无关联呢?
一个死气沉沉的引用,经过赐福,就神气活现地具有了某种灵性,这个词难道用的还不好吗?


同意,我也是这么理解的。
我的理解就直接是bless大致相当于面向对象的构造器,虽然没这回事,但基本就这效果。

[ 本帖最后由 JasonLee8872 于 2007-8-28 00:51 编辑 ]

论坛徽章:
0
发表于 2007-08-28 11:29 |显示全部楼层
狂顶,很到位
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP