Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1138714
  • 博文数量: 53
  • 博客积分: 10025
  • 博客等级: 上将
  • 技术积分: 1640
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-15 17:05
文章分类

全部博文(53)

文章存档

2011年(1)

2010年(3)

2009年(25)

2008年(24)

我的朋友

分类:

2009-04-16 12:40:22

程序说明

网上的很多教程都没有把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


#!/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


#!/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

#!/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


bless.wrong.pl


#!/usr/bin/perl =w

use strict;
use person;
use dog;

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

        # 没有把类型信息和$object绑定,因此无法获知$object有sleep方法

        $object->sleep();
        $object->study();
}

&main();

# 程序运行输出为:


使用c++实现bless的功能

c中的代码

#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


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

阅读(2051) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~