第十二章 对象(下)
来源:互联网 发布:百度云淘宝暗语 编辑:程序博客网 时间:2024/06/05 11:10
第十二章 对象(下)12.5.1 通过 @ISA 继承package Mule;our @ISA= ("Horse", "Donkey");Perl 将首先在Horse 里(和他的任何前辈类里,比如Critter)查找任何在Mule里找不到的方法,找不到以后才继续在Donkey 和其父类里进行查找。如果缺失的方法在一个基类里发现,Perl内部把该位置缓存到当前类里,依次提高效率,当 Perl 搜索一个方法的时候,它确信你没有创建一个闭环的继承级别。如果两个类互相 继承则可能出现这个问题Vsftp:/root/perl/9# perl a1.pl Recursive inheritance detected in package 'Horse' at Critter.pm line 2.Compilation failed in require at a1.pl line 3.BEGIN failed--compilation aborted at a1.pl line 3.使用use base 声明父类:Vsftp:/root/perl/9# cat Horse.pm package Horse;use base ("Critter");sub new {my $invocant = shift;my $class = ref($invocant) || $invocant;my $self = {color => "bay",legs => 4,owner => undef,@_, # 覆盖以前的属性};return bless $self, $class;};sub sum_arr { $self=shift; my $a=shift; my $b=shift; return $a + $b + 7;};1;Vsftp:/root/perl/9# cat Critter.pm package Critter;sub new { my $self = {}; my $invocant = shift; my $class = ref($invocant) || $invocant;my ($name)=@_; my $self = { "name" =>$name }; bless $self, $class; # Use class name to bless() reference return $self;};sub sum { $self=shift; my $a=shift; my $b=shift; return $a + $b;};1;Vsftp:/root/perl/9# cat a1.pl unshift(@INC,"/root/perl/9"); use Horse;;use Critter;use Data::Dumper;$ed = Horse->new; # 四腿湾马print $ed->sum_arr(4,5);print "\n";print $ed->sum(4,5);print "\n";Vsftp:/root/perl/9# perl a1.pl 16912.5.2 访问被覆盖的方法:基类就是父类:Vsftp:/root/perl/9# cat Horse.pm package Horse;use base ("Critter");sub new {my $invocant = shift;my $class = ref($invocant) || $invocant;my $self = {color => "bay",legs => 4,owner => undef,@_, # 覆盖以前的属性};return bless $self, $class;};sub sum { $self=shift; my $a=shift; my $b=shift; return $a + $b + 7;};1;Vsftp:/root/perl/9# cat Critter.pm package Critter;sub new { my $self = {}; my $invocant = shift; my $class = ref($invocant) || $invocant;my ($name)=@_; my $self = { "name" =>$name }; bless $self, $class; # Use class name to bless() reference return $self;};sub sum { $self=shift; my $a=shift; my $b=shift; return $a + $b;};1;Vsftp:/root/perl/9# cat a1.pl unshift(@INC,"/root/perl/9"); use Horse;;use Critter;use Data::Dumper;$ed = Horse->new; # 四腿湾马#print $ed->sum_arr(4,5);print "\n";print $ed->sum(4,5);print "\n";Vsftp:/root/perl/9# perl a1.pl 16此时Horse 类覆盖了基类Critter的 sum方法Vsftp:/root/perl/9# cat a1.pl unshift(@INC,"/root/perl/9"); use Horse;;use Critter;use Data::Dumper;$ed = Horse->new; # 四腿湾马print $ed->Critter::sum(4,5);print "\n";print $ed->sum(4,5);print "\n";Vsftp:/root/perl/9# perl a1.pl 916调用父类Critter::sum sum的方法Vsftp:/root/perl/9# cat a1.pl unshift(@INC,"/root/perl/9"); use Horse;;use Critter;use base qw(Critter);use Data::Dumper;$ed = Horse->new; # 四腿湾马print $ed->SUPER::sum(4,5);print "\n";print $ed->sum(4,5);print "\n";Vsftp:/root/perl/9# perl a1.pl 91612.5.3 UNIVERSAL:最终的祖先类12.5.4 方法自动装载12.5.5 私有方法
0 0
- 第十二章 对象(下)
- 第十二章 5 IO包下的其他对象
- 第十二章对象(上)
- 第十二章 对象(上)
- 第十二章 对象(上)
- 第十二章 对象(上):
- 【翻译】Managed DirectX(第十二章 下)
- 第十二章 泛型算法 (下)
- Windows Shell编程-第十二章.脚本Shell对象
- learn with python-第十二章:类和对象
- 第十二章 C++ 面向对象设计方法概述
- 【软考之旅】第十二章 面向对象
- 第十二章
- 第十二章
- 第十二章
- 第十二章
- 第十二章
- 第十二章..
- mysql 中文乱码奇怪问题
- Windows 7安装 dotnet core SDK 1.1笔记
- java中instanceof用法
- C# Func Action 无重载代理
- 创建ASP.NET Core MVC应用程序(4)-添加CRUD动作方法和视图
- 第十二章 对象(下)
- 开机初始接入
- redis.conf-中文翻译
- spring集成hibernate applicationContext.xml文件中SessionFactory配置
- opencv2-摄像头获取图像相关参数设置
- SAP是啥啊?
- php常用正则表达式收集,正则讲解
- 微信公众号开发中的用户账号绑定
- C# , SQL 数据库 -------------- 连接查询和分组查询 ---- (上机练习)