- 论坛徽章:
- 0
|
package MyPerson;
use strict;
#use warnings;
use vars qw(@ISA);
sub new {
my ($class, @args) = @_;
my ($first,$last,$id,$father,$mother) = @args;
my $self = bless {
'fname' => $first,
'lname' => $last,
'id' => $id,
'mother' => $mother,
'father' => $father,
}, $class;
return $self;
}
sub father {
my ($self,$val) = @_;
return $self->{'father'};
}
sub id { return shift->{'id'} }
1;
#!/usr/bin/perl -w
use strict;
use MyPerson;
my $person = MyPerson->new('jimbo', 'gumbo', 1,2,3);
my @family = (undef,$person); # 0 index needs to be empty
#print "@family\n";
push @family, MyPerson->new('mom', 'gumbo', 3, 0, 0);
print "@family\n","\n";
push @family, MyPerson->new('dad', 'gumbo', 2, 0, 0);
#print "@family\n";
my $dad = $family[$person->father];
print "$dad\n","\n" ;
print "$person\n";
print "father id is ", $person->father, "\n";
print "father obj id is $dad id is ", $dad->id,"\n"
结果:
MyPerson=HASH(0x289f6c) MyPerson=HASH(0x1829fc4)
MyPerson=HASH(0x1829fc4)
MyPerson=HASH(0x289f6c)
father id is 2
father obj id is MyPerson=HASH(0x1829fc4) id is 3
本来想通过程序来体会一下类的工作原理的。没想到 不体会 不知道,一体会 更不知道了。
不明白 怎么出现这么对内存地址。类这部分感觉超级晕!还请不吝赐教。
是不是要学好类 ,引用,模块是重点。
[ 本帖最后由 snss1983 于 2008-9-17 11:32 编辑 ] |
|