Multiple inheritance from a shared base class in Perl Moose

两盒软妹~` 提交于 2019-12-14 03:22:23

问题


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

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

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


回答1:


See Method Dispatch Order in Modern Perl:

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 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.



来源:https://stackoverflow.com/questions/38133739/multiple-inheritance-from-a-shared-base-class-in-perl-moose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!