组合总是比继承更好吗? [英] Composition always better than inheritance?

查看:105
本文介绍了组合总是比继承更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几篇文章,它们主张继承优先于继承,并且我坚信在所有这些情况下,重组可能都是更好的主意.但是继承是否仍然具有比组合有明显优势的方案?我是否还必须再次在组成的类中实现所有属性和方法(如下例所示)?我确定我在这里遗漏了一些东西...

I have read several articles favoring composition over inheritance and I am convinced that in all those cases composition may be better idea. But does inheritance still has scenarios where it has a clear advantage over composition? Also do I have to implement all properties and methods again in a composed class (as shown in example below)? I am sure I am missing something here...

//May have numerous methods and properties
class BaseEngine
{
	...
	Property101 { get; set; }
	...
	Method101 (parameters...)		
	...
}

//Inheritance
Class SpecialEngine
{
	Override Property101 { get; set; }
	Override Method101 (parameters...)
}

//Composition
Class SpecialEngine
{
	BaseEngine baseEngine = new BaseEngine();

	//Do I need to implement all methods and properties again so that they are callable from special engine?
	Property0 { return baseEngine.Property0 }
	Property1 { return baseEngine.Property1 }
	...
	Property100 { return baseEngine.Property0 }
	Property101 { return special_logic; }

	Method0 { return baseEngine.Method0 }
	Method1 { return baseEngine.Method1 }
	...
	Method100 { return baseEngine.Method100 }
	Method101 { return special_logic; }
}

推荐答案

很高兴知道到处都是白痴.

在两种比较"的方法不仅非常重要,而且如果没有任何一种方法绝对不可能的情况下,有一种极端的白痴坚持用"{0}总是比{1}更好"的形式陈述他们.难怪,在极端理性的人由于主题琐碎而宁愿保持沉默的情况下,这类人会遭受强制性文章写作障碍.

这个简单的考虑说明了为什么您看不到主要主题是琐碎的事实"的证明"的文章,如果没有继承和组成,则绝对不可能实现面向对象的编程.不提及没有接口"或没有真正的抽象类",因为没有它们的编程是可能的(并且实际上发生了),尽管很尴尬.

因此,我们会遇到这样一种情况,即出版物中存在明显的愚蠢观点渗透的机制,而实际上没有出版物在争论那些愚蠢的观点.真正有知识的人根本不会认真对待这类出版物.反对他们会损害自己的声誉,但不能改变对固执,更确切地说缺乏思想的看法.

我什至要解释为什么继承很重要吗?您仅提及字段/属性和静态呼叫分派的情况.虚拟方法,后期绑定以及因此的多态性如何?这些是OOP的基石,并且完全基于继承(或者作为虚拟方法,没有继承就没有意义).这些机制的解释和应用可以在任何有关OOP的入门书籍或文章中找到.同时,在将这些机制应用于程序系统的设计中存在严重的设计问题.

阅读任何文章的任何人都应该运用一些批判性思维,并能够看到事物的本质.并不总是那么容易.

—SA
It''s good to know that there are many idiots everywhere.

There is one particular sort of extreme idiots who insist on statements in the form "{0} is always better then {1}" in cases when both "compared" approaches are not only very important, but when things are absolutely impossible without either of them. No wonder, being extreme idiots such people suffer from compulsory article writing disorder in cases when reasonable people would prefer to keep silence due to trivial matter of the subject.

That simple consideration explains why you could not see articles with the main topic devoted to the "proof" of such a trivial fact the object-oriented programming is absolutely impossible without inheritance, as well as without composition. Not mentioning "without interfaces" or "without true abstract classes", because programming without them is possible (and actually took place) albeit awkward.

So, we have a situations when there is a mechanism of infiltration of obviously idiotic opinions in publications while there is virtually no publications arguing against those idiotic opinions; really knowledgeable people simply don''t take such publications seriously. Arguing against them can hurt one''s reputation but cannot change thinking of the stubborn, more exactly, lack of thinking.

Should I even explain why inheritance is important? You mention cases of fields/properties only, and static call dispatch. How about virtual methods, late binding, and, consequently, polymorphism? Those are the cornerstones of OOP and totally based on inheritance (or make no sense without inheritance, as virtual methods). Explanation and application of those mechanisms can be found in any introductory book or an article on OOP. At the same time, there are serious design issues in application of these mechanism to the design of program systems.

Anyone reading any articles should apply some critical thinking and be able to see the essence of things. Not always easy.

—SA


这篇关于组合总是比继承更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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