本文介绍了我可以在 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?

推荐答案

我认为强制实现/重载基类的函数/子类的整个想法对 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 的接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:05