问题描述
每个人似乎都普遍认为原型继承比类继承更简单,更灵活。我在文献中没有看到的,我读过很多关于原型继承的事情的例子,你不能用经典的东西。所以我提出了一个简单的问题:
Everyone seems to generally agree that prototype inheritance is simpler and more flexible than class inheritance. What I have not seen in the literature that I've read is very many examples of things that you can do with prototype inheritance that you cannot with classical. So I pose a simple question:
你可以使用原型继承的一些模式,你不能使用类继承,你可以给出什么样的指导/如果要使用它?
What are some patterns that you can use with prototype inheritance that you cannot with class inheritance and what is the guidance you would give as far when/if to use it?
推荐答案
一个区别(可能至少在概念上)是类继承意味着孩子IS-A父母的类型。原型继承没有这样的含义;哺乳动物是猫的原型(Merriam-Webster定义说这意味着它是模式),但没有别的。猫可以根据需要随意删除/添加/更改行为。
One difference (perhaps at least conceptually) is that class inheritance implies that the child IS-A type of the parent. Prototype inheritance makes no such implication; a mammal is a prototype for a cat (the Merriam-Webster definition says this means it's a "pattern for"), but nothing else. A cat is free remove/add/change behaviors as it sees fit.
这篇关于你可以使用原型继承的模式是什么,你不能用class?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!