免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3902 | 回复: 6
打印 上一主题 下一主题

perl继承学习的两点疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-02-04 11:37 |只看该作者 |倒序浏览
1.new函数支持继承的定义方式:

package:Father:

sub new
{
        my $class = shift;------------------这边具体是什么含义,我发现没有这一行的时候,在子类Sun 的new中也是可以使用$this= Father::new,为什么呢?还属于继承吗?       
            my $this = {};
        bless $this,$class;
        return $this;
}


2.在没有继承的时候,子类怎么找到父类的变量的?
自己的操作:

Package:Father

sub new
{
        my $this = {};
        $this->{'father'}='father';
        bless $this;
        return $this;
}

sub setfathertype
{
        my ($class,$name)=@_;
        $class->{father}=$name;
        print "Set father Type  $name: \n";
}

子类sun调用:
sub new
{
        # my $type=shift;
        my $this= {} ;   ----------这边没有继承
        $this->{'sun'}='Gaoyou';
        bless $this;
        return $this;
}

my $m= new sun;
print "father type is $m->{father} \n";-----------这边是会报错的

如果是先调用了父类的子函数setfathertype ,再使用$m->{father} 是可以的,是什么原因呢?是因为在子函数中使用了变量,加入了hash里面了?那这边应该跟继承是没有关系的吧?
$m->setfathertype(father_nanjing');
print "Now the fathertype is $m->{'father'} \n";


请帮忙解惑下,祝各位新年快乐~~~

论坛徽章:
3
2015亚冠之阿尔希拉尔
日期:2015-08-15 16:33:2215-16赛季CBA联赛之四川
日期:2016-01-03 13:37:0515-16赛季CBA联赛之四川
日期:2016-06-13 15:53:36
2 [报告]
发表于 2016-02-04 11:45 |只看该作者
包名或类名。

论坛徽章:
0
3 [报告]
发表于 2016-02-05 10:15 |只看该作者
回复 2# minirain


很感谢你的解答   
能帮忙针对两个问题给个更详细的解答吗   

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2016-02-05 10:51 |只看该作者
回复 1# 815138698


    把你的完整出错代码贴上来。

论坛徽章:
0
5 [报告]
发表于 2016-02-05 14:41 |只看该作者
回复 4# MMMIX


    可能描述的不清楚,我再重新带上代码说明下我的疑问:

1.  
父类的函数:

package:Father;

sub new
{
        # my $class = shift;
        my $this = {};
        $this->{'Father'}='Father';
        bless $this;
        # bless $this,$class;
        return $this;
}

sub setFathertype
{
        my ($class,$name)=@_;
        $class->{'Father'}=$name;
        print "Set Father Type  $name: \n";
}

子类的函数:
package Sun;
sub new
{
        my $this= {};
        my $this= Father->new;#############继承
        $this->{'Sun'}='Sun';
        bless $this;
        return $this;
}

my $m= new Sun;
print " Father type is $m->{'Father'} \n";

疑问:父类的new函数中这行 my $class = shift;代表是可以被继承的,但是如果没有这一行的时候,子类sun的new() 也写成my $this= Father->new,发现是可行的,而且最后也能通过$m->{Father}访问父类的变量,想知道my $class = shift 这一行有和没有有什么区别吗?  没有的话能不能被继承?



2.子类sun中没有继承Father
sub new
{
        my $this= {};
        $this->{'Sun'}='Sun';
        bless $this;
        return $this;
}


如果我在sun里面直接使用

print "father type is $m->{father} \n";-----------是会报错的,找不到$m->{father}。找不到$m->{father}。

但是如果先调用了父类的子函数setfathertype ,再使用$m->{father} 是可以的,是什么原因呢?是因为在子函数中使用了变量,加入了hash里面了?那这边应该跟继承是没有关系的吧?


$m->setFathertype('Father_nanjing');
print "Now the Father type is $m->{'Father'} \n";----OK

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2016-02-14 10:52 |只看该作者
回复 5# 815138698


   
疑问:父类的new函数中这行 my $class = shift;代表是可以被继承的,


谁说的?

Perl OO 的继承(Inheritance) 参见:http://perldoc.perl.org/perlobj.html#Inheritance

论坛徽章:
0
7 [报告]
发表于 2016-02-27 02:06 来自手机 |只看该作者
虽然我不知道你想说什么,先了解下shift是个什么东西,还有就是bless又起了什么作用,了解了,或者一切都会迎刃而解。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP