【Perl學習筆記】2. perl中的bless理解

bless有兩個參數:對象的引用、類的名稱。 
類的名稱是一個字符串,表明了類的類型信息,這是理解bless的關鍵。 
所謂bless就是把 類型信息 賦予 實例變量。 
程序包括5個文件:
person.pm :實現了person類 
dog.pm :實現了dog類 
bless.pl : 正確的使用bless
bless.wrong.pl : 錯誤的使用bless
bless.cc : 使用C++語言實現了與bless.pl相同功能的代碼c++


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

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

       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;string


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

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

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

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

       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

相關文章
相關標籤/搜索