use*_*842 2 oop perl inheritance moose
我是Perl的新手,并被指示到Moose作为Perl OO的源代码,但我在使其工作方面遇到了一些问题.具体来说,超类的方法似乎没有被继承.为了测试这个,我创建了三个包含以下内容的文件:
thingtest.pl
use strict;
use warnings;
require "thing_inherited.pm";
thing_inherited->hello();
Run Code Online (Sandbox Code Playgroud)
thing.pm
package thing;
use strict;
use warnings;
use Moose;
sub hello
{
print "poo";
}
sub bye
{
print "naaaa";
}
1;
Run Code Online (Sandbox Code Playgroud)
最后,thing_inherited.pm
package thing_inherited;
use strict;
use warnings;
use Moose;
extends "thing";
sub hello
{
bye();
}
1;
Run Code Online (Sandbox Code Playgroud)
所以人们通常会期望方法再见作为子类的一部分继承,但我得到了这个错误...
Undefined subroutine &thing_inherited::bye called at thing_inherited.pm line 11.
Run Code Online (Sandbox Code Playgroud)
谁能解释我在这里做错了什么?谢谢!
编辑:在这样做时,我遇到了另一个难题:从我的超类调用我的基类中的一个方法应该被超类覆盖不会被覆盖.说我有
sub whatever
{
print "ignored";
}
Run Code Online (Sandbox Code Playgroud)
在我的基类中并添加
whatever();
Run Code Online (Sandbox Code Playgroud)
在我的再见方法中,调用再见不会产生覆盖的结果,只打印"忽略".
你有一个函数调用,而不是方法调用.继承仅适用于类和对象,即方法调用.方法调用看起来像$object->method或$class->method.
sub hello
{
my ($self) = @_;
$self->bye();
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,require "thing_inherited.pm";应该是use thing_inherited;