- 论坛徽章:
- 0
|
1.代码如下,好像只找的到当前所属类的函数,但找不到父类的函数:
父类:- 1 package Coco ;
- 2 use Exporter ;
- 3 our @ISA = qw/Exporter/ ;
- 4 our @EXPORT = qw/print set/ ;
- 5 sub new{
- 6 my $x = shift ;
- 7 my $this = {
- 8 name => "Coco" ,
- 9 price => 3 ,
- 10 taste => "cococola" ,
- 11 } ;
- 12 bless $this , $x;
- 13 return $this ;
- 14 }
- 15 sub set{
- 16 my $this = shift ;
- 17 $this{"name"} = $_[0] ;
- 18 $this{"price"} = $_[1] ;
- 19 $this{"taste"} = $_[2] ;
- 20 }
- 21
- 22 sub print{
- 23 my $list = shift ;
- 24 print "this is \n" ;
- 25 for(keys%$list) {
- 26 print "$_ => $list{$_}\n";
- 27 }
- 28 }
- 29 1;
复制代码 子类:- 1 package CocoCola ;
- 2 use Coco ;
- 3 @ISA = qw/Coco/ ;
- 4 @EXPORT = qw/Faker/ ;
- 5 sub new{
- 6 my $x = shift ;
- 7 my $this = Coco::new() ;
- 8 $this->Coco::set("CocoCola","7","helohelo") ;
- 9 bless $this,$x ;
- 10 return $this ;
- 11 }
- 12 sub Faker{
- 13 my $x = shift ;
- 14 print "From FAKER:\n" ;
- 15 $x -> Coco::print() ;
- 16 }
- 17 1;
- 18
复制代码 最终的调用子类的代码:- 1 #!/bin/perl
- 2 @INC = qw/`pwd`/ ;
- 3
- 4 use CocoCola ;
- 5
- 6
- 7 $x = CocoCola::new() ;
- 8 $x->Faker ;
- 9 $x->print ;
- 10 print "\n\n" ;
- 11 print "\n\n\%ma\in:\n";
复制代码 但是最后找得到Faker却找不到print。
2.私有成员函数的构造,书上说是用my声明匿名函数的应用,但是具体怎么用?能不能举个例子?谢谢!! |
|