Rya*_*son 12 methods perl inheritance moose method-modifier
我有一个Moose类,它打算被子类化,每个子类都必须实现一个"execute"方法.但是,我想将一个方法修饰符应用于我的类中的execute方法,以便它适用于所有子类中的execute方法.但是当方法被覆盖时,方法修饰符不会被保留.有没有办法确保我的类的所有子类都将我的方法修饰符应用于它们的执行方法?
示例:在超类中,我有:
before execute => sub {
print "Before modifier is executing.\n"
}
Run Code Online (Sandbox Code Playgroud)
然后,在其子类中:
sub execute {
print "Execute method is running.\n"
}
Run Code Online (Sandbox Code Playgroud)
调用execute方法时,它没有说明"before"修饰符.
这就是augment方法修饰符的用途.你可以把它放在你的超类中:
sub execute {
print "This runs before the subclass code";
inner();
print "This runs after the subclass code";
}
Run Code Online (Sandbox Code Playgroud)
然后,不是让你的子类execute直接覆盖,而是让它们augment:
augment 'execute' => sub {
print "This is the subclass method";
};
Run Code Online (Sandbox Code Playgroud)
基本上它给你的功能就像around修饰符一样,除了父/子关系改变了.
| 归档时间: |
|
| 查看次数: |
929 次 |
| 最近记录: |