我可以在Perl中创建类似Java的接口吗? [英] Can I create Java-like interfaces in Perl?

查看:99
本文介绍了我可以在Perl中创建类似Java的接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Perl的OO模型相当原始;在大多数方面,它本质上是命名空间黑客。

I understand that Perl's OO model is rather primitive; it is, in most respects, essentially a namespace hack.

然而,我想知道是否有可能创建类似接口的东西?我的目标是建立一个扩展其他基类的基类,其主要目的是强制要求这些子类实现某些方法(通过名称很好,不需要签名)。我真的不在乎它是一个纯虚拟类(如Java中的接口)还是具有超类中这些方法的实际实现存根的具体类,但我想要的是确定它是确定性的必要的子类实现了超类的某些方法。

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?

推荐答案

我认为强制实现/重载基类的函数/ subs的整个想法对Perl来说是陌生的。在那一点上你会想到执行机制是否正常工作?

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.

编辑:实际上,是的,Class :: Contract似乎是要走的路。

Actually, yes, Class::Contract seems to be the way to go.

这篇关于我可以在Perl中创建类似Java的接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆