简体   繁体   English

子类使用MooseX :: ABC实现MooseX :: Interface(抽象基类)

[英]Child classes implement a MooseX::Interface with MooseX::ABC (Abstract Base Class)

I am trying to force all child classes of an Abstract Base Class to implement an interface. 我试图强制抽象基类的所有子类实现一个接口。 When I try add with '<interface>' to the ABC, it requires me to implement the required methods immediately and add sub <method> {} for each interface method. 当我尝试在ABC中添加with '<interface>' ,它要求我立即实现所需的方法,并为每个接口方法添加sub <method> {}

To Illustrate the Problem, 为了说明问题,

package drawAPI {
   use MooseX::Interface;
   require 'draw';
   one;
}

package shape {
   use MooseX::ABC;
   with 'drawAPI';
}

package square {
   extends 'shape';
    #here is where having a draw subroutine would be inforced
   sub draw {};
}

Does anyone know a way to do this? 有人知道这样做的方法吗? or can recommend a different set of modules to accomplish this? 还是可以推荐一组不同的模块来实现这一目标?

Thanks 谢谢

The documentation for MooseX::ABC states "NOTE: This module is almost certainly a bad idea. You really want to just be using a role instead!". MooseX :: ABC的文档指出:“注意:这个模块几乎肯定是个坏主意。您真的想只使用角色!”。 The documentation for MooseX::Interface states that "Interfaces are just roles with a few additional restrictions..." Your choice of modules suggests that you want Moose to look and work like Java, which is a lost cause. MooseX :: Interface的文档指出:“接口只是具有一些附加限制的角色……”您所选择的模块表明,您希望Moose看起来和像Java一样工作,这是一个失败的原因。 The good news is that Moose Roles can almost certainly provide much if not all of the behavior you're looking for. 好消息是, Moose Roles几乎可以确定提供您想要的所有行为,即使不是全部。 Furthermore, you might be surprised by some of the powerful things that you can accomplish with Roles that don't have an analog in Java. 此外,您可能会对使用Java中没有类似物的角色可以完成的一些强大功能感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM