从超类调用已知的子类函数?

时间:2013-06-26 18:21:43

标签: perl class oop subclass superclass

我不知道这是否可行,但我想从Perl调用一个已知的子类函数。我需要一些“通用”来称呼更具体的东西。我的超类将假设所有子类的类都定义了已知函数。我想这与Java“implements”类似。

例如,假设我有以下代码:

GenericStory.pm

package Story::GenericStory;

sub new{
   my $class = shift;
   my $self = {};

   bless $self, class;
   return $self;
}

sub tellStory {
   my $self;

   #do common things
   print "Once upon a time ". $self->specifics();
}

Story1.pm

package Story::Story1;
use base qw ( Story::GenericStory );

sub new {
   my $class = shift;
   my $self = $class->SUPER::new(@_);

   return $self;
}

sub specifics {
   my $self;
   print " there was a dragon\n";
}

Story2.pm

package Story::Story2;
use base qw ( Story::GenericStory );

sub new {
   my $class = shift;
   my $self = $class->SUPER::new(@_);

   return $self;
}

sub specifics {
   print " there was a house\n";
}

MAIN

my $story1 = Story::Story1->new();
my $story2 = Story::Story2->new();

#Once upon a time there was a dragon.
$story1->tellStory();


#Once upon a time there was a house.
$story2->tellStory();

编辑:

代码工作正常。我只是忘记了“我的自我=转变”;在tellStory();

2 个答案:

答案 0 :(得分:5)

您的代码可以正常工作(模数琐碎错误);您可能想要添加超类:

sub specifics {
    require Carp;
    Carp::confess("subclass does not implement required interface");
}

或类似。

答案 1 :(得分:0)

您想要的非常像 trait (或Perl:角色)。

Traits是面向对象系统的一个相对新近的补充。它们就像接口一样,它们可以要求继承类来实现某些方法,但它们就像一个抽象的超类,因为它们本身可以提供某些方法。

Moose对象系统允许角色。可以将类声明为with某个角色。这里用MooseX::Declare编写的示例:

use MooseX::Declare;

role Story::StoryTeller{
    requires 'specifics';

    method tellStory() {
        print "Once upon a time ";
        $self->specifics();
    }
}

class Story::Story1 with Story::StoryTeller {
    method specifics() {
        print " there was a dragon\n";
    }
}

class Story::Story2 with Story::StoryTeller {
    method specifics() {
        print " there was a house\n";
    }
}

my $story1 = Story::Story1->new();
my $story2 = Story::Story2->new();

#Once upon a time there was a dragon.
$story1->tellStory();
#Once upon a time there was a house.
$story2->tellStory();

这不是继承:$story1->isa("Story::StoryTeller")为false,但 此角色:$story1->DOES("Story::StoryTeller")为真。

对于每个类DOES某个角色,该类的实例can所有角色的方法。因此,$story1->can("tellStory")为真,而对于每个Story::StoryTeller实例,$instance->can("specifics")都是正确的。

角色无法单独实例化。