我有一个旨在被子类化的Moose类,每个子类都必须实现一个“执行”方法.但是,我想将一个方法修饰符应用于我的类中的execute方法,以便它适用于所有子类中的execute方法.但是当方法被覆盖时,方法修饰符不会被保留.有没有办法确保我的类的所有子类将我的方法修饰符应用于其执行方法?
示例:在一个超类中,我有这个:
before execute => sub { print "Before modifier is executing.\n" }
然后,在一个子类:
sub execute { print "Execute method is running.\n" }
当调用execute方法时,它并没有说明“before”修饰符.
解决方法
这就是增加方法修饰符.你可以把它放在你的超类中:
sub execute { print "This runs before the subclass code"; inner(); print "This runs after the subclass code"; }
然后,而不是允许你的子类直接覆盖执行,你可以增加它:
augment 'execute' => sub { print "This is the subclass method"; };
基本上它给你的功能就像周围的修饰符,除了父/子关系改变了.