我可以在 Perl 中创建类似 Java 的界面吗?
我知道 Perl 的 OO 模型相当原始; 在大多数方面,它本质上是一种命名空间黑客。
尽管如此,我想知道是否有可能创建类似“界面”的东西? 我的目标是拥有一个基类,从中扩展其他类,其主要目的是强制这些子类实现某些方法(通过名称即可,无需签名)。 我真的不在乎它是一个“纯虚拟”类(就像Java中的“接口”)还是一个具有超类中这些方法的实际实现存根的具体类,但我想要的是使它确定性地需要子类实现超类的某些方法。
这可能吗? 如果是这样,怎么办?
I understand that Perl's OO model is rather primitive; it is, in most respects, essentially a namespace hack.
Nevertheless, I wonder if it is possible to create something like an "interface?" My goal is to have a base class from which others are extended whose principal purpose is to make mandatory the implementation of certain methods (by name is fine, no signature necessary) by those subclasses. I don't really care if it's a "purely virtual" class (like an "interface" in Java) or a concrete class with actual implementational stubs for those methods in the superclass, but what I want is to make it deterministically necessary that the subclass implement certain methods of the superclass.
Is this possible? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是使用 Moose 的答案...
现在 size 属性必须是一个实现 Comparable “接口的对象”。 在 Moose-land 中,接口就是角色,而角色不仅仅是接口定义。
Here's an answer using Moose ...
Now the size attribute must be an object which implements the Comparable "interface". In Moose-land, interfaces are roles, and roles can be more than just an interface definition.
我不确定你将如何实施它。 不过,请看一下 Moose,它是“Perl 5 的后现代对象系统”。
I am not sure how you will be able to implement it. However, have a look at Moose, which is "A postmodern object system for Perl 5".
我认为强制实现/重载基类函数/子函数的整个想法对于 Perl 来说是陌生的。 您认为执行机制将在什么时候发挥作用?
如果您可以在运行时执行此操作,那么如果您的基类的实现被调用,您可能会死亡。
编辑:实际上,是的,Class::Contract 似乎是正确的选择。
I think the whole idea of mandating implementation/overloading of base class's functions/subs is foreign to Perl. At which point would you envision the enforcement mechanism working?
If you're OK with doing this at runtime, you can die if your base class's implementation gets called.
EDIT: Actually, yes, Class::Contract seems to be the way to go.
Class::Contract 可以帮忙解决这个问题。 它支持编译时契约检查。
The Class::Contract can help with this. It supports compile-time contract checking.
我有一个称为“兼容”的轻量级模式,我在对 指示一个类是否实现 Perl 中的接口有多重要?
这只是在
中粘贴伪包的问题@ISA
:如果您不按照
X
的预期行事,您就破坏了他们的代码。 在实践中,我有一堆可重复使用的记录行为,但我用一个告诉我它是 X::Compatible 的类来向自己保证它可以达到我的预期。由于 Perl 5.10 引入了
DOES
概念,该概念同样是轻量级的,因此X::Compatible
对象继承自基类Object::Compatible
> 它通过在@ISA
中查找/::Compatible$/
并对其中的任何内容回复肯定来实现基本的DOES
。 这个想法是:I have a lightweight pattern I call "Compatible", and I discuss it in my answer to How important is it to indicate if a class implements an interface in Perl?
It's just a matter of sticking pseudo packages in
@ISA
:You break their code if you don't do what they expect from
X
. In practice I have a bunch of documented behaviors that I reuse, but a class telling me it'sX::Compatible
is what I use to assure myself that it can do what I expect.Since perl 5.10 has introduced the
DOES
concept, which is about as lightweight, aX::Compatible
object inherits from a base classObject::Compatible
which implements a baseDOES
by looking through@ISA
for/::Compatible$/
and replying to the affirmative for anything in there. The idea is that:在运行时产生错误的简单解决方案:
Simple solution that creates errors at runtime: