组合与继承c++
Composition vs Inheritance is one of the frequently asked interview questions. You must have also heard to use Composition over Inheritance.
作文与继承是面试中常见的问题之一。 您还必须听说过使用“继承而不是继承”。
组合与继承 (Composition vs Inheritance)
Both composition and inheritance are object-oriented programming concepts. They are not tied up with any specific programming language such as Java. Before we compare composition over inheritance programmatically, let’s have a quick definition of them.
组合和继承都是面向对象的编程概念 。 它们没有与任何特定的编程语言(例如Java)捆绑在一起。 在以编程方式比较组成与继承之间的关系之前,让我们对其进行快速定义。
组成 (Composition)
Composition is the design technique in object-oriented programming to implement has-a relationship between objects. Composition in java is achieved by using instance variables of other objects. For example, a person who has a Job is implemented like below in java object-oriented programming.
组合物是在面向对象的编程来实现具有-一个对象之间的关系的设计技术。 Java中的合成是通过使用其他对象的实例变量来实现的。 例如,具有工作的人像下面的Java面向对象编程中那样实现。
package com.journaldev.composition;
public class Job {
// variables, methods etc.
}
package com.journaldev.composition;
public class Person {
//composition has-a relationship
private Job job;
//variables, methods, constructors etc. object-oriented
遗产 (Inheritance)
Inheritance is the design technique in object-oriented programming to implement is-a relationship between objects. Inheritance in Java is implemented using the extends keyword.
继承是在面向对象的编程来实现是-一个对象之间的关系的设计技术。 Java中的继承是使用extends关键字实现的。
For example, Cat is an Animal relationship in java programming will be implemented like below.
例如,Cat是动物关系,在Java编程中将像下面那样实现。
package com.journaldev.inheritance;
public class Animal {
// variables, methods etc.
}
package com.journaldev.inheritance;
public class Cat extends Animal{
}
继承而不是继承 (Composition over Inheritance)
Both composition and inheritance promote code reuse through different approaches. So which one to choose? How to compare composition vs inheritance. You must have heard that in programming you should favor composition over inheritance. Let’s see some of the reasons that will help you in choosing composition vs inheritance.
组合和继承都通过不同的方法促进代码重用。 那么选择哪一个呢? 如何比较组成与继承。 您必须已经听说过,在编程中,您应该更喜欢组合而不是继承。 让我们看一些可以帮助您选择组合与继承的原因。
- Inheritance is tightly coupled whereas composition is loosely coupled. Let’s assume we have below classes with inheritance.
package com.journaldev.java.examples; public class ClassA { public void foo(){ } } class ClassB extends ClassA{ public void bar(){ } }
For simplicity, we have both the superclass and subclass in a single package. But mostly they will be in the separate codebase.
There could be many classes extending the superclass ClassA. A very common example of this situation is extending the Exception class.
Now let’s say ClassA implementation is changed like below, a new method bar() is added.
As soon as you start using new ClassA implementation, you will get compile time error in ClassB as
The return type is incompatible with ClassA.bar()
. The solution would be to change either the superclass or the subclass bar() method to make them compatible.If you would have used Composition over inheritance, you will never face this problem. A simple example of ClassB implementation using Composition can be as below.
继承紧密耦合,而组成则松散耦合。 假设下面有带有继承的类。class ClassB{ ClassA classA = new ClassA(); public void bar(){ classA.foo(); classA.bar(); } }
为简单起见,我们在一个包中同时包含了超类和子类。 但大多数情况下,它们将位于单独的代码库中。
可能会有许多类扩展超类ClassA。 这种情况的一个非常常见的示例是扩展Exception类。
现在,让我们说ClassA实现的更改如下,添加了一个新方法bar()。
package com.journaldev.java.examples; public class ClassA { public void foo(){ } public int bar(){ return 0; } }
一旦开始使用新的ClassA实现,您将在ClassB中得到编译时错误,因为
The return type is incompatible with ClassA.bar()
。 解决方案是更改超类或子类的bar()方法以使其兼容。如果您在继承上使用了Composition,那么您将永远不会遇到这个问题。 使用Composition的ClassB实现的简单示例如下。
- There is no access control in inheritance whereas access can be restricted in composition. We expose all the superclass methods to the other classes having access to subclass. So if a new method is introduced or there are security holes in the superclass, subclass becomes vulnerable. Since in composition we choose which methods to use, it’s more secure than inheritance. For example, we can provide ClassA foo() method exposure to other classes using below code in ClassB.
class ClassB { ClassA classA = new ClassA(); public void foo(){ classA.foo(); } public void bar(){ } }
This is one of the major advantage of composition over inheritance.
这是组合相对于继承的主要优点之一。
- Composition provides flexibility in invocation of methods that is useful with multiple subclass scenario. For example, let’s say we have below inheritance scenario.
abstract class Abs { abstract void foo(); } public class ClassA extends Abs{ public void foo(){ } } class ClassB extends Abs{ public void foo(){ } } class Test { ClassA a = new ClassA(); ClassB b = new ClassB(); public void test(){ a.foo(); b.foo(); } }
So what if there are more subclasses, will composition make our code ugly by having one instance for each subclass? No, we can rewrite the Test class like below.
This will give you the flexibility to use any subclass based on the object used in the constructor.
abstract class Abs { abstract void foo(); } public class ClassA extends Abs{ public void foo(){ } } class ClassB extends Abs{ public void foo(){ } } class Test { ClassA a = new ClassA(); ClassB b = new ClassB(); public void test(){ a.foo(); b.foo(); } }
那么,如果有更多的子类,那么每个子类都有一个实例,这种组合会使我们的代码变得丑陋吗? 不,我们可以像下面那样重写Test类。
这将使您能够灵活地使用基于构造函数中使用的对象的任何子类。
- One more benefit of composition over inheritance is testing scope. Unit testing is easy in composition because we know what all methods we are using from another class. We can mock it up for testing whereas in inheritance we depend heavily on superclass and don’t know what all methods of superclass will be used. So we will have to test all the methods of the superclass. This is extra work and we need to do it unnecessarily because of inheritance. 与继承相比,组合的另一个好处是测试范围。 单元测试很容易组合,因为我们从另一个类中知道我们正在使用的所有方法。 我们可以模拟它进行测试,而在继承中,我们严重依赖超类,并且不知道将使用所有超类方法。 因此,我们将必须测试超类的所有方法。 这是额外的工作,由于继承,我们不需要这样做。
That’s all for composition vs inheritance. You have got enough reasons to choose composition over inheritance. Use inheritance only when you are sure that superclass will not be changed, otherwise go for composition.
这就是合成与继承的全部。 您有足够的理由选择组成而不是继承。 仅当您确定不会更改超类时才使用继承,否则请进行合成。
翻译自: https://www.journaldev.com/12086/composition-vs-inheritance
组合与继承c++