请教:关于OOP的问题
请教:关于OOP的问题
将例子简化。
gene1.pm
package Gene1;
use strict;
use warnings;
use Carp;
sub new {
my ($class, %arg) = @_;
return bless {
_name => $arg{name} || croak("no name"),
_organism => $arg{organism} || croak("no organism"),
_chromosome => $arg{chromosome} || "????",
_pdbref => $arg{pdbref} || "????",
}, $class;
}
sub name { $_[0] -> {_name} }
sub organism { $_[0] -> {_organism} }
sub chromosome { $_[0] -> {_chromosome}}
sub pdbref { $_[0] -> {_pdbref} }
1;
testgene1.pl
use strict;
use warnings;
use lib "F:/work/mpfb/lib";
use Gene1;
print "Object 1:\n\n";
my $obj1 = Gene1->new(
name => "Aging",
organism => "Homo sapiens",
chromosome => "23",
pdbref => "pdb9999.ent"
);
print $obj1->name, "\n";
print $obj1->organism, "\n";
print $obj1->chromosome, "\n";
print $obj1->pdbref, "\n\n";
问题:
1, gene1.pm中方法name的输入参数是什么?
sub name { $_[0] -> {_name} }
2,是以数组@_的方式传送的吗?所有几个方法包括new都是一次性的由
my $obj1 = Gene1->new(
name => "Aging",
organism => "Homo sapiens",
chromosome => "23",
pdbref => "pdb9999.ent"
);
来传递参数的吗?
原文说明:The method name receives the object as its first argument because it is called by:
$obj1->name
The body of the subroutine uses the Perl built-in @_ array to access its arguments. The first argument to the subroutine is referred to as $_[0]. That first argument is the object, a reference to a hash, so I give it the key _name to retrieve the desired value:
$_[0] -> {_name}
看不懂为什么这里的参数是数组@_ 而不是Hash或Hash的引用。