另一个继承问题
来自快速谷歌搜索和维基百科关于多重继承的文章,其中引用了:
多重继承是指某些面向对象编程语言的一项功能,其中一个类可以从多个超类继承行为和功能。这与单继承形成对比,单继承中一个类最多可以从一个超类继承。
据我所知,PHP 不允许多重继承。然而,我找不到明确的答案是它是否允许多个类扩展超类。示例:
class a {}
class b extends a {}
class c extends a {}
就我想做的事情而言,我正在制作一个角色扮演游戏,并希望有一个“通用”角色类来包含制作角色模板的所有方法和属性。然后我希望类包含每种类型角色(战士、法师等)的具体信息,例如统计修饰符和特殊攻击。
这可能吗?
From a quick Google search and a the wikipedia article on Multiple Inheritance, which quotes:
Multiple inheritance refers to a feature of some object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. This contrasts with single inheritance, where a class may inherit from at most one superclass.
I understand that PHP doesn't allow multiple inheritance. What I can't find a clear answer on, however, is whether it allows more than one class to extend a superclass. Example:
class a {}
class b extends a {}
class c extends a {}
In terms of what I'm trying to do, I'm making an RPG and want a 'generic' character class to include all the methods and properties that make the template of a character. Then I want classes to include specifics for each type of character (warrior, mage, etc), such as stats modifiers and special attacks.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这是完全可能的。继承的全部目的是多个子级可以从父级继承通用功能。
Yes, it's perfectly possible. The entire purpose of inheritance is that multiple children can inherit common functionality from a parent.
是的,根据您的代码示例,多个类可以扩展同一个类。
Yes, multiple classes can extend the same class, per your code example.
这是OOP的基础。这是有可能的。
This is the foundation of OOP. It's possible.
是的,任何类都可以扩展基类。在您的示例中,类 c 不可能同时扩展 a 和 b。
Yes, any class can extend a base class. In your example, it just isn't possible for class c to extend both a and b.
那么在像上面这样的调整示例中,
c 无法获取 a 的属性吗?
So in an adjusted example like the one above
c could not get to properties of a ?