从Perl Moose中的共享基类进行多重继承 [英] Multiple inheritance from a shared base class in Perl Moose

查看:98
本文介绍了从Perl Moose中的共享基类进行多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让A,B,C,D为驼鹿类.

Let A, B, C, D be Moose classes.

让B和C都从A继承.让D也从B和C继承.

Let both B and C inherit from A. Let also D inherit from both B and C.

重复"属性(B和C中都存在A的属性)会发生什么?

What will happen with "duplicate" properties (properties from A present in both B and C)?

推荐答案

请参见方法调度顺序现代Perl 中:

对于单父类,方法分派顺序(或方法解析顺序或MRO)很明显.先查看对象的类,再查看其父对象,依此类推,直到找到方法或用完父对象为止.从多个父级继承(多个继承)的类(气垫船扩展了Boat和Car)都需要更棘手的调度. 关于多重继承的原因很复杂.尽可能避免多重继承. (强调我的想法)

Method dispatch order (or method resolution order or MRO) is obvious for single-parent classes. Look in the object's class, then its parent, and so on until you find the method or run out of parents. Classes which inherit from multiple parents (multiple inheritance)—Hovercraft extends both Boat and Car—require trickier dispatch. Reasoning about multiple inheritance is complex. Avoid multiple inheritance when possible. (emphasis mine)

Perl 5使用深度优先方法解析策略.它在搜索后继父母的类别之前,递归搜索第一个命名的父母的类别以及该父母的所有父母的类别. mro编译指示(Pragmas)提供了替代策略,包括C3 MRO策略,该策略在搜索给定班级的直系父母之前先搜索其任何父母.

Perl 5 uses a depth-first method resolution strategy. It searches the class of the first named parent and all of that parent's parents recursively before searching the classes of subsequent parents. The mro pragma (Pragmas) provides alternate strategies, including the C3 MRO strategy which searches a given class's immediate parents before searching any of their parents.

这篇关于从Perl Moose中的共享基类进行多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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