本文介绍了C ++的使用Moose与Perl的OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在和麋鹿一起玩,得到一个感觉。我想要一个纯虚拟函数的例子,如C ++,但在Moose语法(特别是在C ++看起来的方式)。我知道即使Moose强加一个比普通Perl更严格的模型,仍然有不止一种方式来做我要求的(通过方法修饰符或 SUPER :: 调用) 。这就是为什么我要求具体的类似于C ++的实现尽可能多。至于为什么?的这个限制?主要是好奇心,但也计划以C ++为中心的人可以大多

I've been playing around with Moose, getting a feel for it. I'd like an example of pure virtual functions like in C++ but in Moose parlance (specifically in a C++-looking way). I know that even with Moose imposing a stricter model than normal Perl, there's still more than one way to do what I'm asking (via method modifiers or SUPER:: calls). That is why I'm asking specifically for an implementation resembling C++ as much as possible. As for the "why?" of this restriction? Mostly curiosity, but also planning to port some C++ code to Perl with Moose in a way that C++-centric people could mostly identify with.

推荐答案

我可以这样使用角色而不是子类化:

I can think of this way using roles instead of subclassing:

{
    package AbstractRole;
    use Moose::Role;
    requires 'stuff';  
}

{
    package Real;
    use Moose;
    with 'AbstractRole';
}

这将给出一个编译错误,因为Real没有 stuff

This will give a compilation error because Real doesn't have stuff defined.

向Real添加 方法现在可以使其正常工作:

Adding stuff method to Real will now make it work:

{
    package Real;
    use Moose;
    with 'AbstractRole';

    sub stuff { print "Using child function!\n" }
}


b $ b

/ I3az /

/I3az/

这篇关于C ++的使用Moose与Perl的OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 23:33