我可以在 Perl 中创建类似 Java 的界面吗?

发布于 2024-07-25 18:47:40 字数 257 浏览 6 评论 0原文

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

转身泪倾城 2024-08-01 18:47:40

这是使用 Moose 的答案...

package Comparable;
use Moose::Role;

requires 'eq';

package Person;

has size => (
    is   => 'ro',
    does => 'Comparable',
);

现在 size 属性必须是一个实现 Comparable “接口的对象”。 在 Moose-land 中,接口就是角色,而角色不仅仅是接口定义。

Here's an answer using Moose ...

package Comparable;
use Moose::Role;

requires 'eq';

package Person;

has size => (
    is   => 'ro',
    does => 'Comparable',
);

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.

断舍离 2024-08-01 18:47:40

我不确定你将如何实施它。 不过,请看一下 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".

北城孤痞 2024-08-01 18:47:40

我认为强制实现/重载基类函数/子函数的整个想法对于 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.

吝吻 2024-08-01 18:47:40

Class::Contract 可以帮忙解决这个问题。 它支持编译时契约检查。

The Class::Contract can help with this. It supports compile-time contract checking.

泪痕残 2024-08-01 18:47:40

我有一个称为“兼容”的轻量级模式,我在对 指示一个类是否实现 Perl 中的接口有多重要?

这只是在 中粘贴伪包的问题@ISA

our @ISA = qw<... X::Compatible ...>;

如果您不按照 X 的预期行事,您就破坏了他们的代码。 在实践中,我有一堆可重复使用的记录行为,但我用一个告诉我它是 X::Compatible 的类来向自己保证它可以达到我的预期。

由于 Perl 5.10 引入了 DOES 概念,该概念同样是轻量级的,因此 X::Compatible 对象继承自基类 Object::Compatible > 它通过在 @ISA 中查找 /::Compatible$/ 并对其中的任何内容回复肯定来实现基本的 DOES。 这个想法是:

$object->DOES( $x ) == $object->isa( $x . '::Compatible' )

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:

our @ISA = qw<... X::Compatible ...>;

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's X::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, a X::Compatible object inherits from a base class Object::Compatible which implements a base DOES by looking through @ISA for /::Compatible$/ and replying to the affirmative for anything in there. The idea is that:

$object->DOES( $x ) == $object->isa( $x . '::Compatible' )
绅刃 2024-08-01 18:47:40

在运行时产生错误的简单解决方案:

package SomeVirtualClass;

use strict;
use warnings;

use Carp;

sub some_method { croak "virtual method some_method not overridden" }

Simple solution that creates errors at runtime:

package SomeVirtualClass;

use strict;
use warnings;

use Carp;

sub some_method { croak "virtual method some_method not overridden" }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文