Perl模块,接受列表并创建对象

Jan*_*kie 1 oop perl module package

我正在研究大学问题(在Perl中).我们正在创建模块,我需要编写一个简单的模块"get/set四个属性的方法:lastname,firstname,full_name和一个也是person对象的子列表".

我想我已经失去了它,但它是那些也是抛弃我的人物对象的孩子.我想模块需要接受一个列表,然后创建一个对象列表?Python是我的核心语言,所以这个让我失望.get/set方法工作正常.有任何想法吗?

我的模块在这里......

#!/usr/bin/perl

package Person;

sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
    };
    bless $self, $class;
    return $self;
}
sub setFirstName {
    my ( $self, $firstName ) = @_;
    $self->{_firstName} = $firstName if defined($firstName);
    return $self->{_firstName};
}

sub getFirstName {
    my( $self ) = @_;
    return $self->{_firstName};
}

sub setLastName {
    my ( $self, $lastName ) = @_;
    $self->{_lastName} = $lastName if defined($lastName);
    return $self->{_lastName};
}

sub getLastName {
    my( $self ) = @_;
    return $self->{_lastName};
}

sub getFullName {
    my( $self ) = @_;
    return $self->{_lastName}.",".$self->{_firstName};
}

1;
Run Code Online (Sandbox Code Playgroud)

我的代码在这里......

#!/usr/bin/perl

use Person;

$object = new Person("Elvis","Presley");
# Get first name which is set using constructor.
$firstName = $object->getFirstName();
$lastName = $object->getLastName();
$fullname = $object->getFullName();

print "(Getting) First Name is : $firstName\n";
print "(Getting) Last Name is: $lastName\n";
print "(Getting) Full Name is: $fullname\n";
Run Code Online (Sandbox Code Playgroud)

cho*_*oba 6

只需使用setter中的对象列表:

#! /usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

{   package Person;

    sub new {
        my $class = shift;
        my $self = {
            _firstName => shift,
            _lastName  => shift,
            _children => [],
        };
        return bless $self, $class
    }

    sub setFirstName {
        my ($self, $firstName) = @_;
        $self->{_firstName} = $firstName if defined $firstName;
        return $self->{_firstName}
    }

    sub getFirstName {
        my ($self) = @_;
        return $self->{_firstName}
    }

    sub setLastName {
        my ($self, $lastName) = @_;
        $self->{_lastName} = $lastName if defined $lastName;
        return $self->{_lastName}
    }

    sub getLastName {
        my ($self) = @_;
        return $self->{_lastName}
    }

    sub getFullName {
        my ($self) = @_;
        return $self->{_lastName} . ', ' . $self->{_firstName}
    }

    sub getChildren {
        my ($self) = @_;
        return @{ $self->{_children} }
    }

    sub setChildren {
        my ($self, @children) = @_;
        $self->{_children} = [ @children ];
    }

}

my $object = 'Person'->new('Elvis', 'Presley');

# Get first name which is set using constructor.
my $firstName = $object->getFirstName;
my $lastName = $object->getLastName;
my $fullname = $object->getFullName;

$object->setChildren('Person'->new('Lisa', 'Presley'),
                     'Person'->new('Deborah', 'Presley'));

say "(Getting) First Name is: $firstName";
say "(Getting) Last Name is: $lastName";
say "(Getting) Full Name is: $fullname";

say "Children: ";
say $_->getFullName for $object->getChildren;
Run Code Online (Sandbox Code Playgroud)

请注意,有一些模块可以使构建对象更容易,例如Moo:

#! /usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

{   package Person;
    use Moo;

    has first_name => (is => 'ro');
    has last_name => (is => 'ro');
    has full_name => (is => 'lazy');
    has _children => (is => 'ro',
                      init_arg => undef,
                      default => sub { [] });

    sub _build_full_name {
        my ($self) = @_;
        return $self->last_name . ', ' . $self->first_name
    }

    sub add_child {
        my ($self, $child) = @_;
        push @{ $self->_children }, $child
    }

    sub children {
        my ($self) = @_;
        return @{ $self->_children }
    }

}

my $object = 'Person'->new(first_name => 'Elvis',
                           last_name  => 'Presley');

# Get first name which is set using constructor.
my $firstName = $object->first_name;
my $lastName = $object->last_name;
my $fullname = $object->full_name;

$object->add_child($_) for 'Person'->new(first_name => 'Lisa',
                                         last_name => 'Presley'),
                           'Person'->new(first_name => 'Deborah',
                                         last_name => 'Presley');

say "(Getting) First Name is: $firstName";
say "(Getting) Last Name is: $lastName";
say "(Getting) Full Name is: $fullname";

say "Children: ";
say $_->full_name for $object->children;
Run Code Online (Sandbox Code Playgroud)


zdi*_*dim 5

该要求意味着应该有一个可以容纳对象集合的属性,因此引用一个数组.这是在构造函数中定义的

sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _children  => [ @_ ],
    };
    bless $self, $class;
    return $self;
}
Run Code Online (Sandbox Code Playgroud)

其中[ ]创建一个匿名数组并返回其引用,这是一个标量,因此它可以用于哈希值.其中@_包含了Person类和名称之后的可选的其余参数(对象)shift.

需要检查参数,但是当它们在位置上使用时,使用普通列表会变得很困难.相反,请考虑使用命名参数,即.将哈希(ref)传递给构造函数,使用它可以更容易地检查哪些参数已经提供或者没有提供.

接下来,您需要一种方法来将子项添加到此属性中

sub add_children {
    my ($self, @children) = @_;             # add checks for what's passed in
    push @{$self->{_children}}, @children;
    return $self;                           # for chaining, if desired
}
Run Code Online (Sandbox Code Playgroud)

最后,当您调用此方法时,您将类的对象传递Person给它

use warnings;
use strict;

use Person;

my $object = Person->new('Elvis', 'Presley');

my $child = Person->new('First', 'Last');

$object->add_children( $child );
Run Code Online (Sandbox Code Playgroud)

或者,如果$child在代码的其余部分中没有使用变量(对象)

$object->add_children( Person->new(...) );
Run Code Online (Sandbox Code Playgroud)

您可以添加子项列表add_children($c1, $c2, ...),例如,最初填充数据结构,或者可以在显示时单独添加它们.

Person也可以在构造函数中使用子列表

my $obj = Person->new('First', 'Last', $c1, $c2,...);
Run Code Online (Sandbox Code Playgroud)

使用提到的命名参数会更清晰,更灵活,这些参数在构造函数中被解压缩和整理.但更重要的是,一旦你学习了Perl的原生OO系统,就会看到这个模块,最好的Moose和它的轻量级Moo.

评论