程序包括5个文件:
CODE:
#!/usr/bin/perl -w
package person; use strict; sub sleep() { print("$name is person, he is sleeping/n"); sub study() { print("$name is person, he is studying/n");
CODE:
#!/usr/bin/perl -w
package dog; use strict; sub sleep() { print("$name is dog, he is sleeping/n"); sub bark() { print("$name is dog, he is barking/n"); return 1;
CODE:
#!/usr/bin/perl =w
use strict; use person; use dog; sub main() # 先把"tom"变为人 # 再把"tom"变为狗 # 最后,再把"tom"变回人 &main(); # 程序运行时输出:
CODE:
#!/usr/bin/perl =w
use strict; use person; use dog; sub main() # 没有把类型信息和$object绑定,因此无法获知$object有sleep方法 &main(); # 程序运行输出为:
CODE:
#include <stdio.h>
#include <stdlib.h> #include <string.h> struct object { struct person { void sleep() { printf("%s is person, he is sleeping/n", this->name); } struct dog { void sleep() { printf("%s is dog, he is sleeping/n", this->name); } #define bless(object, type) ((type*) object) int main() // 先把"tom"变为人 // 再把"tom"变为狗 // 最后,再把"tom"变回人 // 程序运行时输出: |