|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 mengxiao15 于 2018-11-2 15:32 编辑
新手学习perl遇到OOP的问题,想请大神帮忙解答下,现在输出引用的sub为address=HASH...不知道原因。。。。(还有这样引用是不是不规范,想请教下规范的写法,谢谢)
person.pm
package person;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(getName);
sub new {
my $inbocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
name => shift,
};
bless $self, $class;
return $self;
}
sub getName {
my ($self) = @_;
return $self->{name};
}
1;
address.pm
package address;
use person;
sub new {
my $inbocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
house => shift,
post => shift,
};
bless $self, $class;
$self->getName( $self->{name});
return $self;
}
sub getHouse {
my ($self) = @_;
return $self->{house};
}
sub getPost {
my ($self) = @_;
return $self->{post};
}
sub message {
my ($self, $name) = @_;
$self->getName( $name );
}
1;
address.pl
#!/usr/bin/perl -w
use warnings;
use address;
$p = address->new("villa", "100000");
$p->message("Tom");
printf ("House: %s\tPost: %s\t$p", $p->getHouse(), $p->getPost()); |
|