面向对象语言中继承的概念
我和一个朋友讨论多重继承和单继承,发现很明显,我的面向对象设计理念和他完全不同。 我主要是一名 Obj-C 程序员,所以多重继承不是我日常使用的东西。 他主要是 Windows/PSP 下的 C++ 程序员,因此我们可能在日常工作中使用不同的概念。
他其实带来了这样一个话题:新人类继承了什么?
我的想法是,会有一个人类类,新的存在将从该类继承,并从他的两个父母那里获得一些实例变量(例如他的 DNA 和其他)。
他的构想是,孩子要继承父母二人的遗产,才能得到父母的方法。
现在我有点困惑,因为老实说......从对象继承? 继承不是用于从包含特定对象组公共方法的类继承吗? 这个论点真让我困惑不已。
I was discussing multiple inheritance vs. single inheritance with a friend of mine, and discovered that plainly, my conception of Object-Oriented design is completely different than his. I am mostly an Obj-C programmer, so Multiple Inheritance is not something I use daily. He is mostly a C++ programmer under Windows/PSP, so we probably use different concepts on a day-to-day basis.
He actually brought the following subject : What does a new human being inherit from?
My conception of that was that there would be a Human class, and the new being would inherit from that class and get some instance variables (such as his DNA and others) from his two parents.
His conception was that the child would inherit from his two parents, in order to get the methods of his parents.
And now I'm kind of confused, because honestly... Inheriting from objects? Isn't inheritance used to inherit from classes which contain methods common to a certain group of objects? This argument really confused me to no end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
父母也是人类,属于哺乳动物家族的一部分。 你的想法对我来说似乎最合乎逻辑。
当然我看不到:
我看到母亲和父亲相当参与了他们孩子的构建,所有三个都是人类。
The parents are also humans, which are part of the family of creatures called mammals. Your thoughts seem most logical to me.
Certainly I can't see:
I see the mother and father as being rather involved in the construction of their child, all three are humans.
正面打个比方,新人类从父母产生的一对受精卵中获得其特征,而这些受精卵只是松散地基于父母自己的 DNA。
实际上,我认为这只是一个糟糕的类比。 仅仅因为“继承”是从遗传学(或遗嘱和合同法?)借用的术语,并不意味着它必须在概念上与软件开发一一对应,事实上它们只是表面上相似的概念。
例如,我的父亲是一名律师,我的母亲是一名学校老师。 我并不是生来就具备其中任何一种的技能(行为、方法……)。
To take the analogy head-on, the new human gets its traits from a pair of zygotes generated by the parents, which are only loosely based on the parents' own DNA.
Realistically, I think this is just a bad analogy. Just because "inheritance" is a borrowed term from genetics (or wills and contract law?) doesn't mean it has to conceptually match 1:1 with software development, and in fact they are only superficially similar concepts.
For example, my father is a lawyer, and my mother is a school teacher. I was not born with the skills (behaviours, methods...) of either one.
同意 JeeBee 的观点,父母有建设性的作用!
Agree with JeeBee, the parents have a construction role!
你们都错了……孩子和父母都是人类的实例……。 例如,在一种可能的类结构中,HumanBeing(所有这些)都继承自“Primate”,而“Primate”又继承自“Mammal”,而“Mammal”又继承于“Animal”……
人类继承了 Primate 的所有行为,例如“WalkUpright()”等
灵长类继承哺乳动物的所有行为,例如“Lactate()”、“LiveBirth() 等...哺乳动物继承动物的行为...
每个人类实例都可以有两个属性,称为父亲和母亲,每个属性都是HumanBeing...
也许还有另一个名为 Children 的属性,其中包含 HumanBeing 对象实例的集合...
You're both wrong... Both Child and parents are instances of Human Being... . As example, in one possible class structure, HumanBeing (ALL of them) all inherit from "Primate" which Inherits from "Mammal" which Inherits from "Animal"...
Human Being Inherits all behavior of Primate, Like "WalkUpright()", etc.
Primate Inherits all behavior from Mammals, like "Lactate()" "LiveBirth(), etc... Mammal Inherits behavior from Animal...
Each Instance of HumanBeing could have two properties called Father, and Mother, that are each an instance of HumanBeing...
and perhaps another property called Children, that contains a collection of instances of HumanBeing objects...
人是一个阶级,所有的人都属于同一阶级。
孩子、父亲和母亲只是该类的实例。
母亲和父亲只是工厂(人类本身的复合体)
我们可以继承“缺失的链接”< /a> 不过,这取决于域。
但是你的朋友有一个观点,例如阿基里斯继承了宁芙和人类:S,因此证明了多重继承。
超人继承了Kriptonian,而蜘蛛侠则是实现了Spider的人类! :P
A human would be a class, and all humans beings belong to the same class.
The child, the father and the mother are only instances of that class.
The mother and father are just factories ( the the human itself composite )
We may inherit from the "missing link" though, it depends of the domain.
But your friend has a point, Achilles for instance inherit from a Nymph and a Human :S hence multiple inheritance proved.
Superman inherit Kriptonian while Spiderman is a human that implements Spider! :P
好吧,我认为父母也是人。 所以父母是人类的财产。 它与 OOP 继承无关。 如果您指的是以种族为代表的人类类型,例如高加索人延伸人类,则可能会这样做
Well I think the parents are also human. So parents is a property of Human. It has nothing to do with OOP inheritance. It may do so if you are referring to types of human as represented by race such as Caucasian extends Human
就面向对象行为而言,您朋友的观念是错误的。 一个人不会像类那样继承父母的行为。 一个人继承了基因——父母双方各一半——这些基因结合起来就会产生行为。 例如,即使父母双方都有特定的眼睛颜色,孩子也不一定有相同的眼睛颜色(如果您没有上高中生物,请查找隐性基因)。
简而言之,当你考虑类继承时,不要考虑人类继承。 这几乎肯定不会有帮助。
In terms of object oriented behaviour your friend's conception is wrong. A person does not inherit behaviours from their parents in the same way that a class does. A person inherits genes - half from each parent - and these genes combine to cause behaviours. For example even if both parents have a particular eye colour, the child doesn't necessarily have the same eye colour (look up recessive genes if you didn't take high school biology).
In short, don't think about human inheritance when you are thinking about class inheritance. It almost certainly won't be helpful.
简而言之,您和您朋友的继承观念已为许多人所持有,并且同样合法。
您朋友的概念更接近于“基于原型”的 OO 语言(例如 Self 和 Javascript)中的“对象继承”。 你的更符合“类继承”,这是 Smalltalk、Java 和 C++ 的典型特征。
Java(尤其是最近)倾向于将继承视为管理类型的全部内容,现在为了代码重用而提倡“组合”而不是继承,部分原因是继承在 Java 中不如组合灵活,因此“首选组合”是一种很好的做法。 部分原因是“脆弱的基类问题”。
但是,如果您的朋友更习惯于创建“mixins”,他很可能将继承更多地视为一种库包含。 这可能不是 Java 或 C++ 中的最佳实践,但它是人们通常理解继承的一种方式。
The short answer is that both your and your friend's conception of inheritance have been held by many people and are equally legitimate.
Your friend's conception is closer to "object inheritance" in "prototype based" OO languages (eg. Self and Javascript). Yours is more in line with "class inheritance" which is typical of Smalltalk, Java and, yes, C++.
Java (particularly recently) has tended to see inheritance as all about managing types and now urges "composition" instead of inheritance for code-reuse, This is partly because inheritance is less flexible in Java than composition so "prefering composition" is good practice. And partly because of the "brittle base-class problem".
But if your friend is more used to creating "mixins" he may well see inheritance more as a kind of library include. That may not be the best practice in either Java or C++, but it is a way that inheritance has often been understood.