perl中bless的理解

转载自:http://blog.csdn.net/liangrockman/article/details/5674168

bless有两个参数:对象的引用、类的名称。
类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键。
所谓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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值