- 論壇徽章:
- 0
|
回復(fù) 4# MMMIX
可能描述的不清楚,我再重新帶上代碼說明下我的疑問:
1.
父類的函數(shù):
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";
}
子類的函數(shù):
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函數(shù)中這行 my $class = shift;代表是可以被繼承的,但是如果沒有這一行的時(shí)候,子類sun的new() 也寫成my $this= Father->new,發(fā)現(xiàn)是可行的,而且最后也能通過$m->{Father}訪問父類的變量,想知道m(xù)y $class = shift 這一行有和沒有有什么區(qū)別嗎? 沒有的話能不能被繼承?
2.子類sun中沒有繼承Father
sub new
{
my $this= {};
$this->{'Sun'}='Sun';
bless $this;
return $this;
}
如果我在sun里面直接使用
print "father type is $m->{father} \n";-----------是會(huì)報(bào)錯(cuò)的,找不到$m->{father}。找不到$m->{father}。
但是如果先調(diào)用了父類的子函數(shù)setfathertype ,再使用$m->{father} 是可以的,是什么原因呢?是因?yàn)樵谧雍瘮?shù)中使用了變量,加入了hash里面了?那這邊應(yīng)該跟繼承是沒有關(guān)系的吧?
$m->setFathertype('Father_nanjing');
print "Now the Father type is $m->{'Father'} \n";----OK |
|