1

您好,基本上这是 perl 中的 OO 编程问题。我想要两个对象 A 和 B,并且 A 包含一个 B 类型的成员变量。我做了一些测试,但似乎它不起作用。任何的想法?

下午

package a;


sub new{
    my $self = {};
    my $b = shift;
    $self->{B} = $b;
    bless $self;
    return $self;
}

sub doa{
    my $self = shift;
    print "a\n";
    $self->{B}->dob;
}

1;

下午

package b;

sub new {
    my $self = {};
    bless $self;
    return $self;
}

sub dob{
    my $self = shift;
    print "b\n";
} 



1;

测试.pl

use a;
use b;

my $b = b->new;
my $a = a->new($b);
$a->doa;

当我运行它时,它显示:

a
Can't locate object method "dob" via package "a" at a.pm line 16.
4

3 回答 3

6

您忘记了该方法的第一个参数。方法的第一个参数始终是调用者。

sub new {
    my ($class, $b) = @_;
    my $self = {};
    $self->{B} = $b;
    return bless($self, $class);
}

我通常是bless第一个,虽然

sub new {
    my ($class, ...) = @_;
    my $self = bless({}, $class);
    $self->{attribute} = ...;
    return $self;
}

因为它更符合派生类的构造函数。

sub new {
    my ($class, ...) = @_;
    my $self = $class->SUPER::new(...);
    $self->{attribute} = ...;
    return $self;
}
于 2013-02-14T20:43:35.063 回答
4

您可能希望通过使用Moose或其较轻的表亲Moo 之类的东西来使 Perl OO 更容易。您也可能喜欢(免费)Modern Perl Book,以了解更多现代 Perl 必须提供的许多新的和令人兴奋的东西!

#!/usr/bin/env perl

use strict;
use warnings;

package ClassA;

use Moo;

has 'b' => (
  is => 'ro',
  isa => sub { shift->isa('ClassB') or die "Need a ClassB\n" },  # not necessary but handy
  required => 1,
);

sub doa {
    my $self = shift;
    print "a\n";
    $self->b->dob;
}

package ClassB;

use Moo;

sub dob {
  my $self = shift;
  print "b\n";
}

package main;

my $b = ClassB->new;
my $a = ClassA->new( b => $b );
$a->doa;

事实上,根据你想要的,也许你甚至可能想要像委托这样的东西:

#!/usr/bin/env perl

use strict;
use warnings;

package ClassA;

use Moo;

has 'b' => (
  is => 'ro',
  isa => sub { shift->isa('ClassB') or die "Need a ClassB\n" },  # not necessary but handy
  required => 1,
  handles => ['dob'],
);

sub doa {
    my $self = shift;
    print "a\n";
}

package ClassB;

use Moo;

sub dob {
  my $self = shift;
  print "b\n";
}

package main;

my $b = ClassB->new;
my $a = ClassA->new( b => $b );
$a->doa;
$a->dob;
于 2013-02-14T20:42:47.213 回答
2

你没有正确地祝福你的对象。试试这个:

A:

sub new {                                                                                                                                                                                               
    my $class = shift;                                                                                                                                                                                 
    my $b = shift;                                                                                                                                                                                     
    return bless { B => $b }, $class;                                                                                                                                                                  
}  

乙:

sub new {
    my $class = shift;
    return bless {}, $class; 
} 
于 2013-02-14T20:43:45.800 回答