perl中bless的理解
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
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
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liangrockman/archive/2010/06/16/5674168.aspx
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
perl中bless的理解相关推荐
- perl中bless的理解(zz) z
bless有两个参数:对象的引用.类的名称. 类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键. 所谓bless就是把 类型信息 赋予 实例变量. 程序包括5个文件: person. ...
- Perl中删除或替换字符串中特殊字符(如空格)的方法
今天在写实验代码的时候,突然有了这样一个需求,就是用Perl将包含换行(或回车)的字符串变成一行,并且每一行用空格分隔,经过搜索,网友介绍了这样的写法: [plain] view plaincopy ...
- [译]Perl中的数组
原文:http://perl5maven.com/perl-arrays 在本篇Perl教程中,我们将会学习Perl中的数组.本文只是对Perl中的数组进行了一下概述,在以后的文章中,我们还会更加详细 ...
- perl中的文件句柄
文件句柄 文件句柄就是Perl程序里的某个名 称,代表Perl进程与外界之间的输入/输出联系.文件句柄的命名和Perl中其它变量命名没有差别(数字.下划线.字母构成,字母.下划线开头),因没 有任何的 ...
- javascript中 (function(){})();如何理解?
javascript中 (function(){})();如何理解? javascript中: (function(){})()是匿名函数,主要利用函数内的变量作用域,避免产生全局变量,影响整体页面环 ...
- java抽象的理解_Java中抽象类的理解
Java中抽象类的理解 创建 所有 bsp 抽象类 member 初始 dem print ---------------------------------------- ...
- Java中 多态的理解
** Java中 多态的理解 ** 多态官方定义为: 所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指 ...
- 【转载】谈谈我对Java中CallBack的理解
谈谈我对Java中CallBack的理解 转载自: http://www.cnblogs.com/codingmyworld/archive/2011/07/22/2113514.html CallB ...
- C++中关键字的理解--Static
C++中关键字的理解---Static 目录:1.static缘起 2.static自我理解 3.static举例使用 4 ...
最新文章
- python读写二进制
- java基础(参数传递)
- How mBDOC is generated and saved in DB
- 2018-2019 ICPC Northwestern European Regional Programming Contest (NWERC 2018)
- word 2013 题注、图注、插入图片自动修改大小、批量更新题注编号
- Linux内存使用消耗高
- centos 安装gcc
- java加密 js解密_前后端API交互加密解密(js、Java)
- 【音视频】使用DXGI实现多屏幕采集(4-2)
- 一个app的流程分析
- android播放系统音效,Android用SoundPool播放音效
- PHP 获取当天凌晨时间戳
- NOSQL原理,基础用法,NOSQL入门
- Matlab之绘制三维曲面图
- 中国大学MOOC C语言程序设计(大连理工大学) 课后编程题 第十三周题解(个人向仅供参考)
- RAID5系统架构和扩容
- SQLi LABS Less-7 布尔盲注
- 从零开始学习3D可视化之坐标系
- Cisdem Unarchiver for Mac(快速解压缩文件)
- 【小米机试】厨艺大赛奖金