- 论坛徽章:
- 0
|
回复 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 |
|